First of all you need to define how you want to deal with collisions of paths like /register
, /login
, /impressum
and usernames colliding with these paths (like "register", "login" and "impressum"). You probably want to have a blacklist of usernames which are not allowed, because they collide with your pre-defined paths.
To solve the actual problem then, you can create a "catchall" route for /{username}
(maybe with some restrictions on the {username}
part using regex) after all the other routes.
When you request a path in a symfony project, symfony will check the routes in the order they were defined. So if you define your user profile route last (at the bottom of the routing.yml file), it will only be used if all other routes did not match the request.
This means, if you have a user with a name like "register", he would not be able to open his profile page, since the /register
route for the registration page would match the request first.
In Symfony Standard Edition routing is like this:
app:
resource: '@AppBundle/Controller/'
type: annotation
You can't handle route position with annotations, except if you write multiple entries in the routing.yml
file. To make sure the user profile route is last, you should probably remove the annotation for this action and create the route by using yml, like this:
app:
resource: '@AppBundle/Controller/'
type: annotation
user_profile:
path: /{user}
defaults:
_controller: App:User:profile