Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create route for example.com/username in Symfony and preserve all old URLs

Goal:

I want Username URLs like Facebook, Twitter etc.: example.com/Username

Problem:

URLs like /register etc. can not then be reached if I add

   * @Route("/{user}", name="user_profile")

Of course I could have /user/username, but I need for this case /username.

What is the correct way of implementing this?

One idea was adding an event listener for 404 pages which checks if the URL is example.com/abc and check if the username exists. But that seems like a bad hack.

Short question:

How is it possible to make a route www.example.com/username like Facebook, without conflicts with /register etc. in a professional way?

like image 228
emovere Avatar asked Dec 10 '25 03:12

emovere


1 Answers

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
like image 95
Tobias Xy Avatar answered Dec 11 '25 18:12

Tobias Xy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!