We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Routing issue for same pattern url

Hi everybody,

I want to create 2 routes below:

  • For main cate: /{cate:[a-z]{2}} (only have 2 characters from a-z)
  • For sub cate: /{sub_cate:[a-z0-9-]+}-{cate:[a-z]{2}} (normal sub cate slug and end with main cate slug)

But it's not work. Can you give me some solutions?

Route source:

$router->add( "/{cate:[a-z]{2}}", "Category::cate" );

$router->add( "/{sub_cate:[a-z0-9-]+}-{cate:[a-z]{2}}/", "Category::sub" );

Sample:

  • example.com/xd
  • example.com/funny-story-2017-xd

The order of declaration matters, Phalcon router traverses the registered patterns from BOTTOM TO TOP!

Try reordering your add statements:

$router->add( "/{sub_cate:[a-z0-9-]+}-{cate:[a-z]{2}}/", "Category::sub" );
$router->add( "/{cate:[a-z]{2}}", "Category::cate" );


473

I got no luck. Maybe have to process on action in controller!



145.0k
Accepted
answer
edited Jan '17

In your sample:

example.com/funny-story-2017-xd

But in code here:

/{sub_cate:[a-z0-9-]+}-{cate:[a-z]{2}}/

with slash at the end. Try without slash at the end.



473

Thank you, it's ok when remove slash at the end of route define

In your sample:

example.com/funny-story-2017-xd

But in code here:

/{sub_cate:[a-z0-9-]+}-{cate:[a-z]{2}}/

with slash at the end. Try without slash at the end.