Is it possible to make the default parameter value

2019-04-11 07:16发布

I have a route defined in a symfony2 controller using annotations. EG:

@Route("/{year}", name="show_list_for_user", defaults={ "year" = "2012" })

Is it possible to make make the default year dynamic. Maybe to read the year from a service object?

4条回答
女痞
2楼-- · 2019-04-11 07:40

I'm afraid that is not possible, the defaults are static.

查看更多
戒情不戒烟
3楼-- · 2019-04-11 07:47

This is not possible, but workarounds do exist. Create an additional controller which handles the default case.

Method a - forward the request

/**
 * @Route("/recent", name="show_recent_list_for_user")
 */
public function recentAction()
{
    $response = $this->forward('AcmeDemoBundle:Foo:bar', array(
        'year' => 2012,
    ));

    return $response;
}

Method b - redirect the request

/**
 * @Route("/recent", name="show_recent_list_for_user")
 */
public function recentAction()
{
    $response = $this->redirect($this->generateUrl('show_list_for_user', array(
        'year' => 2012,
    )));

    return $response;
}
查看更多
Summer. ? 凉城
4楼-- · 2019-04-11 08:00

Use as default a placeholder, something like

defaults={ "year" = "CURRENT_YEAR" }

then in your controller do something like:

if ($year == "CURRENT_YEAR") {
    $year = //do something to find the current year
}
查看更多
Evening l夕情丶
5楼-- · 2019-04-11 08:01

You can set default parameters in RequestContext.

When Symfony generates an URL, it uses values in this order:

See Symfony\Component\Routing\Generator\UrlGenerator::doGenerate :

$mergedParams = array_replace($defaults,
                              $this->context->getParameters(),
                              $parameters);
  1. user supplied parameters to generateUrl() function
  2. context parameters
  3. route defaults

You can set the context parameters in a request event listener to override Route defaults:

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RouterInterface;

class RequestListener
{
    private $_router;

    public function __construct(RouterInterface $router)
    {
        $this->_router = $router;
    }

    public function onRequest(GetResponseEvent $event)
    {
        $context = $this->_router->getContext();
        if (!$context->hasParameter('year')) {
            $context->setParameter('year', date('Y'));
        }
    }
}

Service configuration:

<service id="my.request_listener"
         class="MyBundle\EventListener\RequestListener">

    <argument id="router" type="service"/>

    <tag name="kernel.event_listener"
         event="kernel.request" method="onRequest" />
</service>

It depends on a use case, if you want to use dynamic default just to generate url, use code above. If you want your controller to dynamically pick right default value before executing action, you could probably use 'kernel.controller' event and set request attributes if not present.

查看更多
登录 后发表回答