2

I have an application and I want to redirect from my routing.yml (not .htaccess) all the routes to the same path:

home:
    path: /
    defaults: { _controller: MyBundle:Default:index }

And if a user enter in / or in /section or even /sajdfñldasf always will redirect to home path.

Is it possible to do this with Symfony2 and routing.yml?

2 Answers 2

8

You need to define parameter that can handle any value:

home:
    path:     /{path}
    defaults: { _controller: MyBundle:Default:index, path: "" }
    requirements:
        path: .*
Sign up to request clarification or add additional context in comments.

3 Comments

Up vote for setting default value for path. What if user hit /somthing/another/another ?
Then path param of request will have value /somthing/another/another
I see, because of path: .*
-1

Yes

for home

home:
    path:     /
    defaults: { _controller: MyBundle:Default:index }

for section

section:
    path:     /section
    defaults: { _controller: MyBundle:Default:index }

For whatever

whatever:
    path:     /whatever
    defaults: { _controller: MyBundle:Default:index }

6 Comments

yes, but without write all the routes, I want the user write whatever he wants and enter directly to the home.
so you have to write that logic in your controller
But if an user enter in '/' or /section/ or 'whatever/ works fine, but If the user enter for example in '/test' will not work
with path: /{section} and in Defaults the same all works fine :)
there is one more solution: You want to create a listener that listens to the kernel.request event (documentation here). In that listener you have access to the request, and the container so you can do anything you like. During kernel.request Symfony gives you a GetResponseEvent. You can set a Response object on this event just as you would return a response in a controller. If you do set a response, Symfony will return it and not go through the normal request --> controller --> response cycle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.