简体   繁体   English

Silex从Twig模板重定向

[英]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. 首先,我知道这是一件可怕的事情,应该只在控制器逻辑中完成,但是我想做的是在Twig中创建一个自定义重定向标签,该标签会触发Silex重定向。

I have a custom node that outputs the following into the doDisplay template: 我有一个自定义节点,将以下内容输出到doDisplay模板中:

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. 基本上,这得到了Twig环境和我创建的扩展,该扩展具有getApp方法,该方法返回Silex $ app变量,该变量包含重定向方法。

But this only returns Array() to the screen. 但这只会将Array()返回到屏幕。 Any suggestions? 有什么建议么?

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. 当您已经发送了200响应以及一个树枝模板作为正文时,就不能将Response对象发送给客户端。 What you could do is inject a meta redirect tag into the page or use JS: 您可以做的是将meta重定向标记注入页面或使用JS:

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM