PROGRAMMING

  minte9
MEMORYApp




R Q
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Front

Lets separate the render template code from the logic.
 
/**
 * Front Controller - public/front.php
 * 
 * We can replace call_user_func argument with any valid callback. 
 * As a convention, for each route, the associated controller ...
 * is $_controller route attribute.
 * 
 * ------------------------------------------------------------
 * 
 * composer require symfony/http-foundation
 * composer require symfony/routing
 * 
 * php -S localhost:8000 public/front.php
 * 
 * http://localhost:8000/hello/Fabien
 *      Hello Fabien
 */

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;

$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app.php';

$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);

try {
    $request->attributes->add(
        $matcher->match($request->getPathInfo())
    );
    $response = call_user_func( // Render Controller
        $request->attributes->get('_controller'), $request
    );

} catch(Routing\Exception\ResourceNotFoundException $e) {
    $response = new Response('Not found', 404);
} catch (Exception $e) {
    $response = new Response('An error occured', 500);
}

$response->send();

Render

This is rather flexible as you can change Response object afterwards.
 
/**
 * App Configuration - src/app.php
 * 
 * This is rather flexible as you can change Response object afterwards.
 */

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

$routes = new RouteCollection();

$routes->add('bye', new Route('bye'));
$routes->add('hello', new Route('/hello/{name}', [
    'name' => 'World',
    '_controller' => 'render_template' // Render Controller
]));

function render_template($request)
{
    extract($request->attributes->all(), EXTR_SKIP);
    ob_start();
    include sprintf(__DIR__.'/../src/pages/%s.phtml', $_route);
    return new Response(ob_get_clean());
}

return $routes;

View

src/pages/hello.phtml
 
Hello <?= $name ?>
Even App

Even App

Lets create a new application that say if a number is even or not.
 
/**
 * public/front2.php
 * 
 * php -S localhost:8000 public/front2.php
 * 
 * http://localhost:8000/isEven/24
 *      Yes, the number is Even
 * http://localhost:8000/isEven/123
 *      No, the number is Odd
 *
 */

ini_set('display_errors', 1);
require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;

$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app2.php'; // Look Here

$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);

try {
    $request->attributes->add(
        $matcher->match($request->getPathInfo())
    );
    $response = call_user_func(
        $request->attributes->get('_controller'), $request
    );
} catch(Routing\Exception\ResourceNotFoundException $e) {
    $response = new Response('Not found', 404);
} catch (Exception $e) {
    $response = new Response('An error occured', 500);
}

$response->send();

App

We add is even logic before render the template.
 
/**
 * Is Even App - src/app2.php
 */

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;

$routes = new Routing\RouteCollection();

$routes->add('isEven', new Routing\Route('/isEven/{number}', [
    'number' => null,
    '_controller' => function($request) {

        $message = "The number is Odd";
        if (isEven($request->attributes->get('number'))) {
            $message = "The number is Even";
        }

        $request->attributes->set('message', $message);
        $response = render_template($request);
        return $response;
    }
]));

function isEven($number) : bool
{
    return $number % 2 == 0;
}

function render_template($request) : Response
{
    extract($request->attributes->all(), EXTR_SKIP);
    ob_start();
    include sprintf(__DIR__.'/../src/pages/%s.phtml', $_route);
    return new Response(ob_get_clean());
}

return $routes;

Questions    
Last update: 254 days ago
Resolver