Symfony How to get defaults values of route by route name

Symfony How to get defaults values of route by route name,symfony,Symfony,I have a route defined as follows. home: pattern: /index/index/{show} defaults: { _controller: MyBundle:MyIndex:mypage, show: all } requirements: show: activeonly|all name: ".+" What I want to do in controller is, $this->forward ('home', array('show', 'activeonly'); I can't find anywhere that this is possible. Looking around, I got the following question and answer. Symfony 2: How to get route defaults by route name? . But the solution given (accepted) doesn

I have a route defined as follows.

home:
    pattern: /index/index/{show}
    defaults: { _controller: MyBundle:MyIndex:mypage, show: all }
    requirements:
        show:  activeonly|all
        name: ".+"

What I want to do in controller is,

$this->forward ('home', array('show', 'activeonly');

I can't find anywhere that this is possible. Looking around, I got the following question and answer.

Symfony 2: How to get route defaults by route name? . But the solution given (accepted) doesn't work. It's because, defaults is returned as private value of an object, which can't be read.

It would be great to know, if there is anyway that I can read _defaults value of a route by it's name and use that value to forward?


#1

Inside your controller use:

$defaults = $this->get('router')->getRouteCollection()->get('home')->getDefaults();

to get the defaults of the 'home' route.

Don't access the defaults by their property _defaults but use the getter Route::getDefaults() instead.

See Symfony2 API: Symfony\Component\Routing\Route::getDefaults()

But as the name "Default value" states, you don't have to specifiy them when using that route because they will be chosen by default. So if you want to forward to your home route simply use:

$this->forward('MyBundle:MyIndex:mypage');

Edit

This is really slow as ::getRouteCollection() doesn't use any caching and calling it will result in a lot of time consuming disk reads and route compilation.

I've answered an alternative approach here (Symfony 2: How to get route defaults by route name?). But be advised: As far as I know, there's still no way to do this efficiently using the official Symfony API. If you really have to do it fast, you need to hack your way through it.


#2

You have to use forward with controller name, not with route.

e.g :

If you want a specific value :

$this->forward ('MyBundle:MyIndex:mypage', array('show' =>'activeonly'));

If you want the default value :

$this->forward('MyBundle:MyIndex:mypage');

If you want to use route, use redirect instead.

$this->redirect($this->generateUrl('home'));

Some info here


#3

Why don't you just call route without parameters as you use default values ?

#4

forward wouldn't work with route name. I can't use redirect (with which I could use the router name) as I have mentioned below, the parameters are dynamic in values and numbers and a lot more (upto 15).

#5

Possible duplicate of Symfony 2: How to get route defaults by route name?

#6

Thanks a lot. That's exactly what I was missing

#7

WARNING: using getRouteCollection() will murder your performance, github.com/symfony/symfony/issues/11171 as it doesn't cache the generated routes

#8

@WiR3D You're absolutely right and I almost forgot about this answer. In 2017 I answered this again in another question (which is older than this one but asking the exact same question): stackoverflow.com/questions/9316222/… - Bottom line: It's not possible to do this efficiently using the Symfony API and if you really need to do it, you still need to hack your way through it.

#9

Very valid point GreenLeaf. But I couldn't do redirect cause, the route can be using POST method as well. And when I experimented, it looks to me that I can use this->forward ('MyBundle\Controller\MyIndexController::mypageAction', array ('show' => 'activeonly')) works, where the first parameter is value which came from router defaults. I hope, I'm not missing anything.