简体   繁体   English

坚持Symfony2形式和学说后重定向

[英]Redirect after persist Symfony2 form and Doctrine

on an web application based on Symfony2 I'm developing a page in which I can insert a new record on my DB. 在基于Symfony2的Web应用程序上,我正在开发一个页面,可以在其中插入数据库中的新记录。 The Insert operation works properly, anyway actually after the confirmation the datas are correctly inserted but the web application return me on the same form that I used to insert the data. 插入操作正常工作,无论如何,实际上在确认正确插入数据之后,但是Web应用程序以与插入数据时相同的形式返回了我。 How can I redirect the page to another route, for example to the page that show me new inserted data? 如何将页面重定向到另一条路线,例如到显示新插入数据的页面?

Here the action in my Controller used to create the new record: 这是我的控制器中用于创建新记录的操作:

        public function newAction(Request $request)
        {
            $em = $this->getDoctrine()->getManager();

            $form = $this->createForm(new AnagraficaType(), new Anagrafiche() );

            if ($request->isMethod('POST')) {
                $form->bind($request);

                if ($form->isValid()) {
$anagrafica = $form->getData();
                    $em->persist($anagrafica);
                    $em->flush();

                }
            }

            return $this->render('AcmeMyBundle:Anagrafica:new.html.twig', array('form' => $form->createView()));

}

Here the part of routing.yml used for the page to create the new record: 这是用于页面以创建新记录的routing.yml部分:

AcmeMyBundle_newAnag:
    pattern: /anagrafica/new
    defaults: { _controller: AcmeMyBundle:Anagrafica:new}

Here the part of routing.yml for the page that permit me to show detailed a single record of my 'anagrafica' objects in DB: 这是该页面的routing.yml部分,它允许我详细显示DB中我的“ anagrafica”对象的单个记录:

AcmeMyBundle_showAnag:
    pattern:  /anagrafica/{id}
    defaults: { _controller: AcmeMyBundle:Anagrafica:show }
    requirements:
        _method:  GET
        id: \d+

Here the action in my controller that manage the page to show detailed a single record of my 'anagrafica' objects in DB: 这是我的控制器中管理页面的操作,用于详细显示DB中我的“ anagrafica”对象的单个记录:

public function showAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $anagrafica = $em->getRepository('AcmeMyBundle:Anagrafiche')->find($id);

    if (!$anagrafica) {
        throw $this->createNotFoundException('Unable to find anagrafica post.');
    }

    return $this->render('AcmeMyBundle:Anagrafica:show.html.twig', array(
        'anagrafica'      => $anagrafica,
    ));
}

This is the view new.html.twig: 这是new.html.twig视图:

{% extends 'AcmeMyBundle::layout.html.twig' %}

{% block title %}{% endblock %}

{% block body %}
{{ form(form) }}

{% endblock %}

This is the Form Type: 这是表单类型:

<?php
namespace Acme\MyBundle\Form\Type;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class AnagraficaType extends AbstractType
    {
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Acme\MyBundle\Entity\Anagrafiche',
                'cascade_validation' => true,
            ));
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {

            // Create the form
            $builder->add('nome', 'text', array('label' => 'Nome: ', 'required' => false));
            $builder->add('cognome', 'text', array('label' => 'Cognome: ', 'required' => false));
            $builder->add('data_nascita', 'date', array('label' => 'Data nascita: ', 'required' => false));
            $builder->add('luogo_nascita', 'text', array('label' => 'Luogo Nascita: ', 'required' => false));
            $builder->add('indirizzo', 'text', array('label' => 'Indirizzo: ', 'required' => false));
            $builder->add('telefono', 'text', array('label' => 'Telefono: ', 'required' => false));
            $builder->add('email', 'text', array('label' => 'Email: ', 'required' => false));
            $builder->add('save', 'submit');

        }

        public function getName()
        {
            return 'anagrafica';
        }
    }

I need to extract the id of the new persisted record, and automatically redirect to the /anagrafica/$id route (AcmeMyBundle_showAnag) after the confirmation of the form. 我需要提取新的持久记录的ID,并在表单确认后自动重定向到/ anagrafica / $ id路由(AcmeMyBundle_showAnag)。

Suggestion? 建议? Thanks. 谢谢。

Modify your code as given below: 修改您的代码,如下所示:

public function newAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $form = $this->createForm(new AnagraficaType(), new Anagrafiche() );

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            $anagrafica = $form->getData();
            $em->persist($anagrafica);
            $em->flush();
            return $this->redirectToRoute('AcmeMyBundle_showAnag', array('id' => $anagrafica->getId()));            
        }
    }

    return $this->render('AcmeMyBundle:Anagrafica:new.html.twig', array('form' => $form->createView()));
}

So that, once the entity is persisted, it will redirect you to its show page. 这样,一旦实体被保留,它将把您重定向到其显示页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM