【问题标题】:Symfony2 Route prefix querystring dynamicSymfony 路由前缀查询字符串动态
【发布时间】:2015-09-15 17:11:22
【问题描述】:

我的 symfony2 项目设置有正常的 YAML 路由到任何正常的项目。

路由是用注释设置的,最终 URL 是

http://examplecom/artices/{id}

http://example.com/comments/{id}

我想为所有路径添加前缀查询字符串,前提是存在名为preview的查询字符串

所以如果我访问http://example.com/?preview=something - 我希望这个查询字符串附加到所有路由,所以它会继续在每个页面上传递,如果它不存在,那么它将继续照常使用。

我怎样才能做到这一点?

【问题讨论】:

    标签: symfony url routes request twig


    【解决方案1】:

    service.yml

    parameters:
        router.options.generator_base_class: "Acme\\DemoBundle\\Routing\\Generator\\UrlGenerator"
    

    UrlGenerator.php

    <?php
    
    namespace Acme\DemoBundle\Routing\Generator;
    use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
    
    class UrlGenerator extends BaseUrlGenerator
    {
        protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens)
        {
            return parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens).'?preview=something';
        }
    }
    

    参考:http://h4cc.tumblr.com/post/56874277802/generate-external-urls-from-a-symfony2-route

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多