-
Notifications
You must be signed in to change notification settings - Fork 2
Routing
Routing in SpiffyFramework is handled through the SpiffyRoute component. The router is only responsible for matching the request to a dispatchable (Action).
By default, each package loads routes using the routes.php
file in config/routes.php
. Below is a simple example of the config/routes.php
file.
// file: package_name/config/routes.php
// 'route_name' => ['route_path', 'Action/Service Name', ['additional options']]
return [
'home' => ['/', 'Application\Home\IndexAction'],
];
A basic route consists of a name, path, and dispatchable which can be either an action or a service.
'home' => ['/', 'Application\Home\IndexAction']
This route is named home
, responds to the /
and invokes Application\Home\IndexAction
.
'home' => ['/', 'application.home.index']
This route is identical to the one above assuming application.home.index-action
is a service definition for Application\Home\IndexAction
.
Routes can include tokens which get passed to actions automatically.
'articles/view' => ['/articles/{id}']
Tokens, by default, are required. This route would respond to /articles/1
and /articles/foo
but not /articles
.
Routes can constrain the tokens using a regular expression.
'articles/view' => ['/articles/{id:\d+}']
This route would respond to /articles/1234
but not /articles/foo
.
Finally, tokens can be optional.
'articles/view' => ['/articles/{id:\d+}{-slug?}']
This route would respond to /articles/1234-foo
and /articles/1234
but not /articles/foo
. The ?
at the end of slug denotes an optional token.
Note: The dash (-) is included inside the optional token. If it was outside the token it would be required for the route to match. e.g., /articles/1234-
would match but /articles/1234
would not!
The last argument for creating routes is an array containing any additional options which are outlined below.
Key | Description | Example |
---|---|---|
defaults | Allows you to assign a default value to tokens that were not given in the route. | ['defaults' => ['slug' => 'foo-bar']] |
methods | Forces the route to match only if the HTTP method matches. | ['methods' => ['get']] |
© 2014 Kyle Spraggs