Routing
Routing configuration is defined in application modules in the file "etc/routing.php", located under the module's root
directory. This file must return a class that implements Amarant\Framework\Contract\Routing\RoutingConfiguratorInterface.
Important
In production mode, routing configuration is cached under "var/cache" in the application root directory. If changes are made, this cache has to be deleted for the new configuration to take effect.
Create a route
Path
Specifies the route's path.
Controller
The controller class. By default, the "__invoke" method will be called to execute the controller.
If a different method should be called, specify it by adding "::" and the method name.
For example:
Name
A unique name for the route.
Important
Use "_" , "." and alphanumeric characters only as route names. This is very important for page layouts whose
filenames are determined using the route name. The layout builder converts all "_" to "." and expects the layout files
to have a very specific naming convention.
Area
Frontend, backend or global. Specifying a route to be global makes it available in both frontend and backend area.
Methods
A list of HTTP methods the route should support.
Route parameters
To specify route parameters, add them in the path wrapped in curly brackets, like so:
To add parameter requirements, use a regular expression. For example, to require the parameters to be digits only:
To add an optional part of the path, wrap it in square brackets:
Warning
The optional part can only be the last part of the path.
Parameter resolving and injection
All route parameters will be resolved after the route is matched and passed onto the controller's executing method. The controller method should expect these parameters to all be in camelcase.
This means route parameters "param-a", "param-b" will be passed onto the method as "paramA", "paramB", which also means the method's signature should look something like this:
Or something like this if the parameters are supposed to be only digits:
The method can also declare additional parameters to be injected, for example this will inject the current request context:
The order of additional parameters in the method signature does not matter.
Additional parameter requirements
Route parameters can be additionally validated using regex or closures specified in route's "paramRequirements".
paramRequirements: [
// closure
'param-a' => fn ($param) => \is_string($param) && \str_contains($param, 'x'),
// regex
'param-b' => '/[^0-9]/',
]
Access and data scopes
To require the user to have specific access or data scopes to be able to access the route, add the following to route's "metadata":
metadata: [
// require the user to have these access scopes
Route::METADATA_REQUIRED_ACCESS_SCOPES => ['access_scope_a', 'access_scope_b'],
// require the user to have access to the default data scope
Route::METADATA_REQUIRED_DATA_SCOPES => [
\Amarant\Framework\Config\DefaultScopeContextProvider::createScopeValue(
\Amarant\Framework\Contract\Config\ScopeInterface::DEFAULT_SCOPE,
\Amarant\Framework\Contract\Config\ScopeInterface::DEFAULT_SCOPE
)
]
]
Default status code
To set a default status code a response should have when this route is matched, add the following to route's "metadata":
Note
This status code is applied only to layout results returned from controllers.
Hidden routes
Sometimes you may want a route to be able to forward to, but don't want the users to be able to visit it.
To make the router skip a route, add the following to route's "metadata":
Grouping
Routes can be added to groups.
- Unique group name. If the group already exists, it won't be added to the collection. You can check and fetch the existing group using hasGroup and getGroup on the route collection.
- Optional path prefix. The paths of all routes in a group will be prefixed by this value.
Notice how we now add the route to the group and not to the collection. If we should add it to the collection, it would also become a part of a group, but the default one, which has no prefix.
Note
A route is matchable no matter if their path ends with a slash (/) or not. When adding a base route to a group with a prefix, you could declare the group's prefix ending without a slash, and then setting the route's path to just "/".
Menu
To add menu entry for a route:
- Unique menu group id. If a group exists, it will be overwritten. Check if a group exists using getMenuGroup first.
- The group's title, usually rendered as a dropdown menu title.
- Sort order used when rendering menu groups.
- Optional icon class, usually added to the group's title as an "<i>" element.
- Route menus, by default, are meant to be used in the backend (back office).
- The menu group id under which the menu entry will be rendered, usually as a link within a dropdown menu item.
- The title of the entry, usually the title of the link.
- Sort order used when rendering menus inside a menu group.
Note
The menu is rendered only in the backend (back office) theme Hadron. It's possible to use this functionality in the frontend, but you have to render it yourself and set the area parameter of such menu items to frontend.