Recommend this page to a friend! |
Classes of Gjero Krsteski | PIMF | manuscript/6c.-Routing.md | Download |
|
DownloadRoutingThe PIMF Framework helps you map resource URIs to specific controller/action. A PIMF application will invoke the first route that matches the current HTTP request’s URI and method. If the PIMF application does not find routes with URIs that match the HTTP request URI and method, it will automatically return a 404 Not Found response. PIMF just merges variables from request string into the same parameters array where other variables are stored. Also there is a default routes pattern that maps default routes like this /:controller/:action/:id. Basically you have two options – use defaults and custom routes or use only custom, which means that all other requests will be ignored. Custom routes can be defined at your routes.php which lives at your application directory. Routing ConfigurationAll routing configuration lives at config.app.php at the sector Application meta which you can set your custom URI options for your application:
Setting routeable to false you will got URIs like "http://localhost/?controller=blog&action=show" otherwise setting routeable to true you will got cleaner URIs like "http://localhost/blog/show" Routing restrictionFollowing route patterns are PIMF Framework restriction, which gives you enough combinations to describe your application:
If your controller class name is Blog and your action name is searchAction than PIMF will automatically map /blog/search to it! Route ParametersYou can embed parameters into route resource URIs. In this example, we have one parameter in our route URI :slug.
To create a URL parameter, prepend “:” to the parameter name in the route URI pattern. When the route matches the current HTTP request, the values for each route parameter are extracted from the HTTP request URI and are passed into the associated controller/action in order of appearance. Wildcard route parametersYou may also use wildcard route parameters. These will capture one or many URI segments that correspond to the route pattern’s wildcard parameter into an array. A wildcard parameter is identified by a “+” suffix; it otherwise acts the same as normal route parameters shown above. Here’s an example:
When you invoke this example application with a resource URI “/hello/Barry/White”, the route name argument will be equal to array('Barry', 'White'). Route ConditionsPIMF lets you assign conditions to your route parameters. If the specified conditions are not met, the route will not run. For example, if you need a route with a second segment that must be a valid 4-digit year, you could enforce this condition like this:
The third parameter at \Pimf\Route is $conditions which is an associative array with keys that match any of the route’s parameters and values that are regular expressions. Optional route parametersThese are ideal for using one route for a blog archive listing. To declare optional route parameters, specify your route pattern like this:
Each subsequent route segment is optional. This route will accept HTTP requests for:
Currently, you can only use optional route segments in situations like the example above where each route segment is subsequently optional. You may find this feature unstable when used in scenarios different from the example above. Redirect to routeIt is easy to redirect the client to another URL with the PIMF application’s controller redirect() method. This method accepts two arguments. The first argument is the route like controller/action to which the client will redirect; By default the redirect() method will send a 302 Temporary Redirect response.
Or if you wish to use a permanent redirect, you must specify the destination route as the first parameter and the the second parameter to true.
This method will automatically set the Location: header. The HTTP redirect response will be sent to the HTTP client immediately. Route URL HelperPIMF delivers you a PHP native helper function url($route = '', array $params = array(), $https = null, $asset = false) which you can use it at your controller or within your .phtml templates to compute your desired URL:
|