Basit
Basit

Reputation: 17184

Symfony2 Route prefix querystring dynamic

My symfony2 project is setup with normal YAML routes to any normal project.

Routes are setup with annotation and final URLs are

http://examplecom/artices/{id}

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

I want to add prefix querystring to all the path, only if there is querystring called preview

So If I access http://example.com/?preview=something - I want this querystring to append to all the routes, so it continue to pass on every page and if this does not exist, then it will continue to be used as normally.

How can I accomplish this?

Upvotes: 2

Views: 294

Answers (1)

Basit
Basit

Reputation: 17184

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';
    }
}

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

Upvotes: 1

Related Questions