Sami
Sami

Reputation: 755

symfony2 : apply different template with special parameter?

How can I apply different template (view) when I type ?t=1 in the url ?

Examples :

mysite.com -> opens home with default template, let's say home.twig
mysite.com/?t=1 -> opens home with another template : home1.twig (or 1/home.twig)

Is it possible ?

Thanks :)

Upvotes: 0

Views: 23

Answers (1)

Cristian Bujoreanu
Cristian Bujoreanu

Reputation: 1167

Sure, you can define different routes for this, but second url should look like "mysite.com/1":

route_1:
    pattern: /
    defaults: { _controller: AppBundle:Index:home }

route_2:
    pattern: /{t}
    defaults: { _controller: AppBundle:Index:custom }
    requirements:
        t: '[0-9]+'

A second approach is to load different views based by existence of "t" parameter:

public function homeAction(Request $request)
{
    $view = $request->query->has('t') && $request->query->get('t') == 1 ? 'home1.twig.html' : 'home.twig.html';
    return $this->render($view, $params);
}

Upvotes: 1

Related Questions