Routes
Routing is the act of matching a 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. Routing can utilize other portions of the request URI or environment as well – for example, the host or scheme, query parameters, headers, request method, and more.
In Crater, all router must be declared in Bootstrap.php:
namespace App; class Bootstrap extends \Core\Bootstrap { public function setRoutes() { return array( array('any', '/', '\Controllers\Hello@index'), array('post', '/insert-post', '\Controllers\Blog@insertPost') ); } }
In the example above, at line 6, is declared '/' route. This route, call index action from Hello controller. First element of array is type of request method, which can be 'post' or 'get' ('any' can also be used to match both post and get requests) to match the HTTP action. To call a route to a controller instead of typing a function instead enter a string. In the string type the namespace of the controller (\Controllers if located in the root of the controllers folder) then the controller name, finally specify what method of that class to load (without 'Action'). They are dictated by a @ symbol.
Routing Filters
Routes can use filters to dynamically pass values to the controller, their are 3 filters:
- any - can use characters or numbers
- num - can only use numbers
- all - will accept everything including any slash paths
To use a filter place the filter inside parenthesis and use a colon inside route path
array('get', '/product/(:any)', '\Controllers\Product@view'),
Would get past to App/Controllers/Product.php anything after product/ will be passed to view method.
public function viewAction($param) {
Consoles routes
Please read more about consoles on console page.
↑