gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:29:54 26 4
gpt4 key购买 nike

在基于 Symfony2 的 Web 应用程序上,我正在开发一个页面,我可以在其中向我的数据库插入一条新记录。 Insert 操作正常工作,无论如何实际上在确认数据已正确插入之后,但 Web 应用程序以我用于插入数据的相同形式返回给我。如何将页面重定向到另一个路径,例如显示新插入数据的页面?

这里是我 Controller 中用于创建新记录的操作:

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

}

这里routing.yml中用于页面创建新记录的部分:

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

这里是页面的 routing.yml 部分,它允许我显示数据库中我的“anagrafica”对象的详细单个记录:

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

这里是我的 Controller 中的操作,它管理页面以显示数据库中我的“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,
));
}

这是 View new.html.twig:

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

{% block title %}{% endblock %}

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

{% endblock %}

这是表单类型:

<?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';
}
}

我需要提取新的持久记录的id,并在确认表单后自动重定向到/anagrafica/$id 路由(AcmeMyBundle_showAnag)。

建议?谢谢。

最佳答案

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

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

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

关于php - 在坚持 Symfony2 形式和学说后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com