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.
Routing is configured at the module level. For a module
Application, the configuration will be located at
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:
- matches domains and subdomains.
- matches an exact URI path.
- matches HTTP verbs.
post,putfor a route that submits a form.
- allows crafting a tree of possible routes based on segments of the URI path.
/blogcan have child routes with
/subscribe, which would match
- matches a URI path using a regular expression.
/product/001, with the
- matches the URI scheme.
- matching one or more segments of a URI path.
/blog/why-use-mvc, with the
Learn more about routing in the laminas-router documentation.