Middleware controllers act as a handler for multiple routes. Some conventions were made:
action
route parameter(possibly optional) anywhere inside the route(e.g /user[/{action}]
)Action
. For example a route and action pair like /user/forgot-password
will be converted to method forgotPasswordAction
.index
, so you should always define an indexAction
within your controllers for displaying a default page or redirecting.In order to create your action based controllers, you must extend the abstract class DotKernel\DotController\AbstractActionController
Creating a UserController with default action and a register action. Will handle routes /user
and /user/register
use DotKernel\DotController\AbstractActionController;
class UserController extends AbstractActionController
{
public function indexAction()
{
//...
}
public function registerAction()
{
//...
}
}
Then register this controller as a routed middleware in file RoutesDelegator.php
just like a regular middleware.
//Example from a DotKernel RoutesDelegator
$app->route(
'/user[/{action}]',
UserController::class,
[RequestMethodInterface::METHOD_GET, RequestMethodInterface::METHOD_POST],
'user'
);
Use case: You have defined a controller inside some package, with default actions. You want to add actions that fall into the same controller name(or route name more exactly). You want to do this without extending the controller provided by the package. In this case you can do the following
Now when a request for this route comes in, your controller will run first. DotKernel controllers are designed to ignore requests that cannot be matched to one of its methods, so if no action matches, it will call the next middleware, in our case, the second controller. If this is the last controller, and action does not match here, it will go to the default 404 Not found page(handled by NotFoundDelegate)