How should I route in KOHANA?

How to route domain.com/locale/controller to domain.com/controller in Kohana?

  • I'm trying to implement localization in my website. Currently, the basic (English) website is at http://domain.com/controller/action and I want each localization to be at http://domain.com/locale/controller/action. Basically, if a user visit the latter URL, Kohana will use the same controller and action than for the English version. In code, I will simply swap the strings. Currently, I tried by adding the following route but that didn't work: // This is my default route: Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); // This the route for the localizations: Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); With this setup, if I visit http://domain.com/es/controller/action, I will get a 404 error. Any idea how I should setup my routes to make this work? Edit: Just to complete matino and John Himmelman's answer, if I simply swap the rules as suggested, it will work. However, the "locale" route would then become the catch-all route and you will always have to specify the locale, even if all you need is the default one (in my case "en" / English). To fix that, you can limit the "locale" parameter to the locales you support. For example: Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('locale' => '(fr|zh|en)', 'overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); In that case, only URLs that start with "fr", "zh" or "en" will be supported. Additionally, unsupported locales will return a 404 errors, and "domain.com/controller/action" will correctly display the default, English locale.

  • Answer:

    Kohana applies routes in the order they appear in your bootstrap. This is why your default/catch-all route should always be defined last. From http://kohanaframework.org/3.0/guide/kohana/routing doc: It is important to understand that routes are matched in the order they are added, and as soon as a URL matches a route, routing is essentially "stopped" and the remaining routes are never tried. Because the default route matches almost anything, including an empty url, new routes must be place before it. As suggested, swapping routes will resolve the issue. // This the route for the localizations: Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); // This is my default route: Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?')) ->defaults(array( 'controller' => 'home', 'action' => 'index', ));

Laurent at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.