For example, lets say I have this route.
<?php declare(strict_types = 1);
$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $router) {
$router->addRoute('GET', '/{slug}', ['App\Controllers\SomeController', 'someMethod']);
}, [ 'cacheFile' => ROOT . '/storage/cache/route.cache', 'cacheDisabled' => true, ]);
Here is how I handle routes, and call the controller and its method.
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
echo '404 Not Found';
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
echo '405 Method Not Allowed';
break;
case FastRoute\Dispatcher::FOUND:
$controller = $dice->create($routeInfo[1][0]);
echo $controller->{$routeInfo[1][1]}($routeInfo[2]);
break;
}
How do I pass {slug} to the controller method? It doesn't mention anything about it in its documentation, and no information about it can be found via a google search.
I didn't work with DICE until now, though I looked in its implementation in order to present the first option to you. I hope it will work. If not, feel free to read the DICE documentation/code regarding the
callrule and thecreatemethod.Note: The title should be something like "FastRoute: Pass route parameters to handler", or "FastRoute: Pass route arguments to handler", because a prefix is defined as the route part which is prepended to each route pattern inside of a route group.
Option 1: Use the
callrule of the DI container (DICE):This is, of course, the recommended way, since the DI container automatically injects the method arguments. Which could be more than the ones read from the route!
Note: controller method =: "action".
See (in DICE docs):
callrule in 3. Configuring the container with Dice Rules.Route:
Note: if you have optional route parts, then you have to define the corresponding action parameters as optional.
Dispatching request by FastRoute:
Action in UserController:
Option 2: Call the action (without DICE), separately passing all route parameters to it:
Route:
The same.
Dispatching request by FastRoute:
Action in UserController:
Option 3: Call the action (without DICE), passing an instance of a Request class:
Assign the route parameters list to a
Requestinstance (See PSR-7), as attribute, and pass the instance as action argument.Route:
The same.
DI container definitions:
Dispatching request by FastRoute:
Action in UserController: