Need help with changing the regulars for routing.

Route::set('default', '(<controller>(/<action>(/<id>)))', array( 'id' => '[\d]+', )) ->defaults(array( 'controller' => 'home', 'action' => 'index', 'id' => NULL, )); 

This route corresponds to URLs like / , /product/ , /product/add/ , /product/add/123 . (<controller>(/<action>(/<id>))) is an expression, followed by an array of expressions to check for the compliance of the parameters, the default values ​​of the parameters are passed to the defaults.

How to change the route:

 Route::set('pages', '(<lang>/<slug>(/<subrequest>))', array( 'lang' => '[az]{2}', 'slug' => '[\w\-]+', 'subrequest' => '[\w\-\/]+', )) ->defaults(array( 'controller' => 'Page', 'action' => 'index', 'lang' => 'en', 'slug' => 'home', 'subrequest' => NULL, )); 

So that:

  • lang could be set or omitted
  • slug could be set or omitted
  • subrequest could be set (if slug set) or omitted

those. links to this route would fit: / , /en/ , /en/contacts/ , /contacts/ , /en/contacts/config/meta/ and /contacts/config/meta/

    1 answer 1

    For your case it is better to use 2 routes. It will not be possible to throw out the first parameter so that the second and third ones remain.

    In this scenario, there may be either all, or necessarily the first 2 parameters, and the optional third.

     (<lang>/<slug>(/<subrequest>)) 

    It is necessary to make 1 mandatory, the second is necessary and the third is also the last

     <lang>(/<slug>(/<subrequest>)) 

    And create a second route below, since it is more general, but without a language variable

     <slug>(/<subrequest>) 

    There is nothing terrible if it is used for 1 route more, then your task will be solved, especially since when the development is finished and the application takes on the desired look and functionality it will be necessary to cache the routes to reduce the load. It is necessary to cache in any case if the routes are more than 15 - 20 pieces

    And in theory, if you need to use languages, it is better to leave the language variable always mandatory.

     <lang>(/<slug>(/<subrequest>)) 

    You can try to set this way in this way, only in this case it will be better to set the language options in the range of acceptable values:

     Route::set('pages', '(<lang>/)(<slug>(/<subrequest>))', array( 'lang' => '(ru|en)', // и так далее 'slug' => '[\w\-]+', 'subrequest' => '[\w\-\/]+', )); 

    But for some reason it seems to me Kohana will not understand such a mockery of her logic =)

    • mandatory language can not be done, because This route will not work for the main page. why does it seem to me here you can still do with one route and my ignorance of regexp affects me - Anton Shamanov