Symfony 2:302 http 状态和异常 [英] Symfony 2 : 302 http status and exception

查看:66
本文介绍了Symfony 2:302 http 状态和异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我不知道如何解决.

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 "http://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 状态和异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆