For example There is such a route and url

http://mysite/test/article-1 

and

 // Route on Test Route::set('Test', 'test/<alias>',array('alias'=>'\D+')) ->defaults(array( 'controller' => 'Test', 'action' => 'article', )); // Route on Test Pages Route::set('Test', 'test/<alias>',array('alias'=>'\d+')) ->defaults(array( 'controller' => 'Test', 'action' => 'index', )); 

this will only work for http: // mysite / test / article, how to do it more correctly and so that alias does not begin with a digit?

    1 answer 1

    Do you always have an article ? Then:

     Route::set('Test', 'test/article-<id>', array('id'=>'[0-9]+')) // test/article-123 // параметры ['id' => '123'] 

    If not, then:

     Route::set('Test', 'test/<link>-<id>', array('link' => '[az]+', 'id'=>'[0-9]+')) // test/hello-123 // параметры ['link' => 'hello', 'id' => '123'] 

    Or all at once:

     Route::set('Test', 'test/<alias>', array('alias'=>'[az]+-[0-9]+')) // test/hello-123 // параметры ['alias' => 'hello-123'] 

    Writing \D not worth it, because it will capture everything that is not a number, incl. and slashes that can add surprises to you in processing routes. For example, in the case of:

     Route::set('Test1', 'test/<alias>', array('alias'=>'\D+')) Route::set('Test2', 'test/<alias>/details', array('alias'=>'[az]+-[0-9]+')) 

    .. the second route (Test2) will never work.

    • one
      article-<id> , no, this is an example given, it can be like this: test/ar2-3tickl2-1-abcd , the main thing for me is not to start with a digit, for example. like this: test/1 or test/2 , because this is how the route to the page goes. - Smash
    • the fact of the matter is that it is not necessary to mix, action'y different should be. - Smash
    • one
      so, from this point on in more detail - where are your actions in the route? - xEdelweiss
    • 2
      well, so what's the difference? write a rule like array('alias' => '[az]+[0-9a-z-]+') in the route under the article and get your ar2-3tickl2-1-abcd - xEdelweiss alias field
    • one
      Right, thanks @xEdelweiss! - Smash