Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



30.7k
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.