PHP

  minte9
REMEMBERS




Last update:   27-03-2022

Resolver

Lets convert our controller into a proper class, lazy-loaded.
 
/**
 * Front Controller - public/front.php
 * 
 * The render controller is always instantiated, 
 * even if URL doesn't match the route.
 * 
 * It will be bettter if the controllers are lazy-loaded.
 * We will use HttpKernel for that.
 * 
 * Argument resolver getArguments() inspects the controller signature.
 * It determines which arguments to pass using PHP reflexion.
 * 
 * ------------------------------------------------------------
 * 
 * composer require symfony/http-foundation
 * composer require symfony/routing
 * composer require symfony/http-kernel
 * 
 * php -S localhost:8000 public/front.php
 * 
 * http://localhost:8000/isEven/24
 *      The number is Even
 * http://localhost:8000/isEven/123
 *      The number is Odd
 */

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

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;

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

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

try {
    $request->attributes->add(
        $matcher->match($request->getPathInfo())
    );

    $ctrResolver = new ControllerResolver(); // Look Here
    $argResolver = new ArgumentResolver();
    $controller = $ctrResolver->getController($request);
    $arguments = $argResolver->getArguments($request, $controller);

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

$response->send();

Class

A controller resolver knows what controller to execute.
 
/**
 * Is Even App - src/app2.php
 * 
 * $_controller can be any valid PHP callback.
 * 
 * A controller resolver knows what controller to execute, based on the Request.
 * It supports supports strings composed of a class name followed by two colons.
 */

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

$routes = new RouteCollection();
$routes->add('isEven', new Route('/isEven/{number}', [
    '_controller' =>  'CheckNumberController::index', // Look Here
]));

class CheckNumberController
{
    public function index ($number)   // Look Here
    {
        if ($this->isEven($number)) {
            return new Response("The number is Even");
        }
        return new Response("The number is Odd");
    }

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

return $routes;

Questions     github Framework
SoC
        A B C D E F
🔔
1/2