2

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?

1 Answer 1

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

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

Sign up to request clarification or add additional context in comments.

Comments

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.