On this page
Reference
Routing
Routing is the act of matching an HTTP request to a given controller.
Typically, routing will examine the request URI, and attempt to match the URI path segment against provided constraints. If the constraints match, a set of matches are returned, one of which should be the controller name to execute:
'home' => [
'type' => Laminas\Router\Http\Literal::class,
'options' => [
'route' => '/home',
'defaults' => [
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
],
],
],
Routing can utilize other portions of the request URI or environment as well, such as the host or scheme, query parameters, headers, or request method.
Configuration File
Routing is configured at the module level. For a module Application
, the configuration will be located at module/Application/config/module.config.php
:
return [
'router' => [
'routes' => [
'home' => [
'type' => Laminas\Router\Http\Literal::class,
'options' => [
'route' => '/home',
'defaults' => [
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
],
],
],
// additional routes
],
],
];
Note that when adding multiple routes, the last route in the list will be checked first.
HTTP Route Types
Laminas MVC ships with the following HTTP route types:
- Hostname
- matches domains and subdomains.
Example:docs.laminas.dev
. - Literal
- matches an exact URI path.
Example:/contact-us
- Method
- matches HTTP verbs.
Example:post,put
for a route that submits a form. - Part
- allows crafting a tree of possible routes based on segments of the URI path.
Example:/blog
can have child routes with/rss
and/subscribe
, which would match/blog/rss
and/blog/subscribe
respectively. - Regex
- matches a URI path using a regular expression.
Example:
/product/(?<id>[0-9]+)
would match/product/001
, with theid
parameter containing001
. - Scheme
- matches the URI scheme.
Example:https
. - Segment
- matching one or more segments of a URI path.
Example:/:blog/:article
would match/blog/why-use-mvc
, with thearticle
parameter containingwhy-use-mvc
.
Learn more about routing in the laminas-router documentation.