php – 在twig模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在twig模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有索引操作,打印1)表单用于创建新实体; 2)所有实体的清单:
public function indexAction()
{
    $em = $this->getDoctrine()->getEntITyManager();

    $entities = $em->getRepository('MyBundle:Entity')->findAll();

    return array(
        'entities' => $entities,);
}

枝条:

{% block content %}
    {% render "MyBundle:Entity:new" %}
    {% render "MyBundle:Entity:list" %}
{% endblock %}

实体控制器中的newAction是标准形式的控制器:

public function newAction()
{
    $entity = new Entity();
    $form = $this->createForm(new EntityTyPE(),$entity);

    $request = $this->getRequest();
    if ($request->getmethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            // NOT WORK
            return $this->redirect($this->generateUrl('entity_show',array('id' => $entity->getId())));
        }
    }
    return array(
        'form' => $form->createView(),);
}

实体持久化后重定向不起作用,错误

An exception has been thrown during the rendering of a template
("Error when rendering "http://example.COM/app_dev.PHP/url/"
(status code is 302).") in MyBundle:Default:index.htML.twig at line 2.
@H_403_18@ 不幸的是,这是设计的.您无法从嵌入式控制器重定向.作为一种变通方法,您可以发布到不同的URL,保存引荐来,在那里做你需要的东西,然后重新定向结果.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在twig模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向全部内容,希望文章能够帮你解决php – 在twig模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。