我有这个问题,我不知道如何解决.
I have this problem I dont know how to resolve.
我在控制器操作方法中,我进行了一个测试,如果这个测试是肯定的,我想重定向.
I am in a controller action method , I make a test and if this test is positive I want to redirect.
public function contentAction() { $response = $this->render('MyBundle:Default:content.html.twig'); if(!is_object($agent)) { $response = $this->redirect($this->get('router')->generate('new_route', array('error_message_key' => $error_message))); } return $response; }使用重定向,我有一个例外
With the redirection, I have an exception
Error when rendering "alternativefirst/app_dev.php/accueil/" (Status code is 302)我无法进行此重定向?为什么会这样?
I cannot make this redirection ? why could it be ?
重要事项:我在树枝模板中调用此控制器操作
IMPORTANT THING : I call this controller action in a twig template with
{{ render(controller('MyBundle:Default:content')) }} 推荐答案解决方案是将渲染和重定向逻辑移到 /accueil 控制器操作中,因为就目前而言,您重新尝试在已加载的页面上执行 PHP 标头重定向,这是不可能的.或者,您可以通过执行以下操作在您尝试加载的 content.html 模板中执行 javascript window.location 重定向:
The solution would be to move the rendering and redirection logic into the /accueil controller action because as it is right now, you're attempting to perform a PHP header redirect on a page that has already been loaded, which isn't possible. Alternatively, you could perform a javascript window.location redirect inside the content.html template you're trying to load by doing something like this:
{% if agent is not defined %}<script>window.location = '{{ path('new_route') }}';</script>{% endif %}您必须传递代理对象(或任何您想用作触发器的对象),但在我看来,这是一个相当粗略的解决方案,可以解决您的代码中可能存在的架构问题.
You would have to pass along the agent object (or anything you want to use as the trigger), but that strikes me as a rather crude solution to what is likely an architectural problem in your code.
更多推荐
Symfony 2:302 http 状态和异常
发布评论