D
I usually do as follows:Collection - The collection that will save all routes.Route - The class representing a route. You must provide information such as the uri you want to capture and accepted http verbs.Router - The class that serves to bridge between Route and Collection. It is a facilitator to create routes within the collection.Dispatcher Interface - The interface that provides a method to run as the route dispatcher. I would prefer to interface in order to be able to meet various implementations, for example, someone who wants to use a more complex library or who simply wanted to use less resources.So, in that context, we would have a structure more or less like this:$router = new Router(new Collection);
// Cria a instância de Route, coloca dentro da Collection
// E retorna Rota recém-criada, para possíveis outra definições
$router->get('/', 'HomeController::getIndex');
$router->get('/login', function () {})->setName('home.login');
// Classe que implementa DispatcherInterface
$router->dispatch(new Dispatcher());
Based on the context presented in the question, I believe that an interesting would be:$request = new Request();
// retorna new Response se tudo der certo
$response = $router->dispatch(new RequestDispatcher($request));
$response->send();
Inside your RequestDispatcher, you could apply the due operations to call Controller and Method.For example: class RequestDispatcher implements DispatcherInterface {
public function __construct(Request $request) {
$this->request = $request;
}
public function dispatch(Router $router) {
$route = $router->findByRequest($this->request);
if (! $route) return $this->notFound();
$response = call_user_func($route->callAction(), $route->getParameters());
return $response;
}
}
That is, in your Dispatcher, you can add the necessary operations to look for the route within the collection. When not found, you can invoke an action for lacing error 404. When it is found, you can call the action set to the given url and call it, then returning a Response. Trying to sum it up: Dispatcher searches for the route within a collection of routes, which was created by the router. Then if Dispatcher finds the route, it converts the return of the route action (It can be a method of a Controller or a Closure) to a Response, which is finally sent to the exit.You quoted in your question about a Handler, which is passed through a callback. I think you're talking about an anonymous function that some libraries often use to "love" a certain functionality for a route in order to be executed at the end.In my library I did that, too. I can use both a method of a class (the Controller) and an anonymous function. I think this is very useful in cases where the route does not make sense to point to a specific controller, because it is not something that has no "relationship" with the rest.Example: $router->get('/json/cep/{num}', function ($number) {
$url = sprintf('https://cep.correios.com.br/%s.json', $number);
$dados = json_decode(file_get_contents($url));
// Transforma em JsonResponse
return $dados;
});