Symfony 2:302 http 状态和异常

编程入门 行业动态 更新时间:2024-10-09 03:18:43
本文介绍了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 "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 状态和异常

本文发布于:2023-10-10 18:03:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   状态   Symfony   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!