加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:25:40 所属栏目:PHP教程 来源:网络整理
导读:我有索引操作,打印1)表单用于创建新实体; 2)所有实体的清单: public function indexAction(){ $em = $this-getDoctrine()-getEntityManager(); $entities = $em-getRepository('MyBundle:Entity')-findAll(); return array( 'entities' = $entities,);} 枝
我有索引操作,打印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.
不幸的是,这是设计的.您无法从嵌入式控制器重定向.作为一种变通方法,您可以发布到不同的URL,保存引荐来源,在那里做你需要的东西,然后重新定向结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读