Meet the sixth part of a series of articles on development with Kohana PHP V3 (KO3). Previous parts can be found under the tag "
familiarity with kohana 3.0 ". In this part we will look at routing.
Routing is, in fact, receiving a request, analyzing it and redirecting it to the right place. Something like the built-in analogue of the .htaccess file. Why may need routing, you ask? There can be a lot of reasons, so I’ll only give you a few examples of how to set up routes for different scenarios.
What does the route look like? Open the “bootstrap.php” file from the “application /” folder and scroll to this comment:
Below should be something like:
Route::set('default', '(<controller>(/<action>(/<id>)))') ->defaults(array('controller' => 'welcome', 'action' => 'index'));
That is, we already have a pre-configured route. Let's break it down:
1. name of the route - “default”
2. '((/ (/)))' - brackets make optional the indication of the controller, action and id. And the signs <...> indicate that this parameter should be assigned to a variable with the name specified in angle brackets.
3. the default controller is “welcome”, and the default action is “index”.
')
If you go to the address “http: // localhost / mykohana3 /”, the router will see that we have not specified either the controller or the action. He's about going through the routes for a match. Since we have a default route (“default”), the router will select it and add “welcome” instead of the missed controller, and “index” instead of the missed action. That is, if you enter “http: // localhost / mykohana3 /” in the browser, it will be similar to “http: // localhost / mykohana3 / welcome / index”.
If you enter the address ”http: // localhost / mykohana3 / hmvc /”, then the router will notice that the controller is specified and the action is not. Therefore, it will call the “index”, which is specified as the default action.
In addition to the controller and action there is an “id” parameter, which is also optional. If we go to the address “http: // localhost / mykohana3 / hmvc / index / 111”, we will not see anything new, since we have not written any handler for this parameter. But if we add the second parameter: “http: // localhost / mykohana3 / hmvc / index / 111/222”, then we get an error. This is because only three segments are registered in our route (controller / action / id), and “/ 222” is the fourth. We can fix this by bringing the route to the following form:
Route::set('default', '(<controller>(/<action>(/<id>(/<overflow>))))', array('overflow' => '.*?')) ->defaults(array('controller' => 'welcome', 'action' => 'index'));
If you save 'bootstrap.php' and refresh your browser, the error should disappear.
We added an additional array “array ('overflow' => '. *?')”. He assigns a regular expression to the “overflow” argument, thereby telling the router to accept everything, including slashes, and pass it on to the controller as the variable “overflow”. So after the third section, the router will no longer accept the text after each new slash as a separate parameter.
Let's go back to our id argument. If we are going to use it as, for example, the digital identifier of the article, then it is worth giving an error if the user tries to transfer something else. One way to solve this problem is to create (in our case, a change) route:
Route::set('default', '(<controller>(/<action>(/<id>(/<overflow>))))', array('id' => '[[:digit:]]{1,}', 'overflow' => '.*?')) ->defaults(array('controller' => 'welcome', 'action' => 'index'));
If we now open the browser “http: // localhost / mykohana3 / hmvc / index / xxx”, we will see an error about the impossibility of finding a route, which is essentially similar to the 404th page. Of course, you can arrange such a check inside the controller, this is just an example of the possible use of routes.
Now let's create our own route instead of changing the standard one. Add the following to the “bootstrap.php” file:
Route::set('monkeys', 'monkeys(/<action>(/<id>))') ->defaults(array('controller' => 'ko3', 'action' => 'posts'));
Now, when opening the page “http: // localhost / mykohana3 / monkeys”, the action “posts” of the controller “ko3 ″ will be called. In the list of sections, the word “monkeys” is not framed by round brackets, which makes it not optional. This route is called when the router sees the word “monkeys” in place of the controller.
Here is an example of a route that can be used to assign static pages (borrowed from the
Unofficial Kohana 3.0 Wiki ):
Route::set('static', '<page>', array('page' => 'about|faq|locations')) ->defaults(array('controller' => 'page', 'action' => 'static'));
Routing has other options, but this article should cover most situations in which you may need to use it.
Translator's note: on Habré there is another article about routing in Kohana 3.0 .