Following this tutorial assumes that you already completed the installation and configuration steps.
Get an instance of the Dot\Router\RouteCollector using the below code:
/** @var \Dot\Router\RouteCollectorInterface $routeCollector */
$routeCollector = $container->get(\Dot\Router\RouteCollectorInterface::class);
Create a route group by setting the route group prefix and optionally a(n array of) common middleware(s):
$routeCollector->group('/resource', SomeMiddleware::class);
group method arguments:
/Each call to group() returns a new group.
Create groups this way rather than fetching Dot\Router\RouteGroupCollectorInterface from the container: the container returns the same shared instance every time, so its prefix and middleware would leak between modules.
$excludeMiddleware removes group middleware only; the route's own middleware and handler always run.
A group middleware given as a class name is excluded when that exact name is listed.
A group middleware given as an object is excluded when it is an instance of a listed class or interface.
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->route('/manage', ResourceHandler::class, 'route-name', ['GET', 'POST']);
If this route does not need to be piped through specific middleware(s), you can use the fifth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->route('/manage', ResourceHandler::class, 'route-name', ['GET', 'POST'], excludeMiddleware: SomeMiddleware::class);
route() method arguments:
any())$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->any('/manage', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->any('/manage', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
any() method arguments:
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->delete('/{id}', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->delete('/{id}', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
delete() method arguments:
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->get('/{id}', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->get('/{id}', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
get() method arguments:
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->patch('/{id}', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->patch('/{id}', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
patch() method arguments:
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->post('', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->post('', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
post() method arguments:
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->put('/{id}', ResourceHandler::class, 'route-name');
If this route does not need to be piped through specific middleware(s), you can use the fourth argument, $excludeMiddleware, to exclude those group middlewares at route level (makes sense when there are multiple routes in a group):
$routeCollector->group('/resource', [SomeMiddleware::class, OtherMiddleware::class])
->put('/{id}', ResourceHandler::class, 'route-name', excludeMiddleware: SomeMiddleware::class);
put() method arguments:
getRoutes() on a group returns every route registered in the application's route collector, not only the routes of that group.