travis.paxton
travis.paxton

Reputation: 75

Silex redirect from Twig template

First, I know this is a horrible thing to do and should be done in controller logic only, but what I'm trying to do is create a custom redirect tag in Twig that fires a Silex redirect.

I have a custom node that outputs the following into the doDisplay template:

return $this->getEnvironment()->getExtension('silex')->getApp()->redirect('/', 301);

Basically this gets the Twig environment and the extension I created which has a getApp method which returns the Silex $app variable, which contains the redirect method.

But this only returns Array() to the screen. Any suggestions?

Upvotes: 1

Views: 8317

Answers (1)

Dean
Dean

Reputation: 6354

What you're asking to do is impossible. You can't send a Response object to the client when you have already sent a 200 response along with a twig template as the body. What you could do is inject a meta redirect tag into the page or use JS:

For example:

{% if somecondition %}
<script>window.location.href = {{yourRoute}}</script>
{% endif }

Upvotes: 2

Related Questions