I'm using the Symfony router component. The router tells you if a defined route match with an incoming request and return you an array with the infomartion of that route(in my case I have defined a controller and method). I wanna know which is the best practice to execute that method. Right now I'm doing something like this:
<?php
require 'vendor/autoload.php';
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpFoundation\Request;
$routes = new RouteCollection();
$routes->add('test', new Route(
'/test/{id}',
['_controller' => 'app\controllers\testController::test'],
['id' => '[0-9]+'],
[],
'',
[],
['GET']
)
);
$context = new RequestContext();
$context->fromRequest(Request::createFromGlobals());
$matcher = new UrlMatcher($routes, $context);
try {
$parameters = $matcher->match($context->getPathInfo());
call_user_func_array($parameters['_controller'], array_slice($parameters, 1, -1));
} catch (ResourceNotFoundException $e) {
echo $e->getMessage();
}
?>
This works but I don't know if this is the correct way to do it.