2

on an web application based on Symfony2 I'm developing a page in which I can insert a new record on my DB. 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. 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:

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:

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:

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:

{% 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.

Suggestion? Thanks.

4

1 回答 1

5

修改您的代码,如下所示:

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()));
}

这样,一旦实体被持久化,它就会将您重定向到它的显示页面。

于 2013-09-30T11:45:50.657 回答