php - URL alias or replace with symfony2

936

I have the application which generates urls like this

ex)

http://myserver.com/shows/12

http://myserver.com/shows/14

My routing.yml is

acme_top_writeReviewSchool:
    pattern:  /show/{userId}
    defaults: { _controller: AcmeTopBundle:Default:showUser}   

12 or 14 is id used in database.

id| name | position
12| james| pitcher 
14| nick | short
15| ian  | catcher

Now,what I would like to make alias for this urls

http://myserver.com/james

http://myserver.com/nick

or

http://myserver.com/u=james

http://myserver.com/u=nick

or

http://myserver.com/show/james

http://myserver.com/show/nick

I have googled with keywords like 'alias' 'url exchange' or something, howevere can't find good example or bundle.

Could you help me?

11

Answer

Solution:

You can actually have multiple routes pointing to the same bundle:controller:action.

For example if you want to use http://myserver.com/james

acme_top_writeReviewSchool_by_name:
pattern:  /{name}
defaults: { _controller: AcmeTopBundle:Default:showUser}  

The only thing you have to be aware of is that in the controller action you need to either pass the Request object to get either the param "name" or "userId", Alternatively you could name the param the same and manage it by hand (so to speak).

Could you post the controller action here also?

People are also looking for solutions to the problem: php - How to delete this items in laravel5 (array)?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.