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

How can i call the specified controller, when the the first route part is a user name in my database?

I need to use a specific url structure: mydomain.com/username -> show the user carton. maydomint.com/username/title -> show the part of the user data.

Thx.



43.9k
Accepted
answer

Hi,

phalcon's routeer is your friend

example here

Thanks for the quick answer. I know the route, but to this solution, the first part of route is fix. example from your example: /confirm/{code}/{email} the confirm part is fix. There is another solution without fix route part? If not, i will use /user or something before the username.

in any event, thank you

The easiest solution would be to put /user as the first part of the URL. However, I can understand the appear for mydomain.com/username.

What you can do is make your first rule a catch-all rule that treats all requests tha get to it as a request for a user page. Remember that Phalcon's router matches the last route first, so your first route will be matched last. For example:

$Router->add('rule for usernames'); // checked last
$Router->add('rule for confirm'); // checked second
$Router->add('rule for email'); // checked first

You can make your rule regex really generic, then specify the "user" controller when that route is matched.