我正在用Symfony 4.1开发我的第一个项目。我在Twig中用于呈现表单的控制器中的函数中的redirectToRoute存在问题。函数正确,但是提交表单后出现错误:渲染模板时引发了异常(“渲染“ http://localhost:8000/pl/contact/agro/时出错”(状态代码为302)。在哪里可以找到解决方案问题吗?也许没有路由头就无法在此操作中使用redirectToRoute函数吗?
我的ContactController
class ContactController extends Controller
{
public function renderForm(Request $request, $currentBrand)
{
$formData = new ContactEmail();
$form = $this->createForm(ContactFormType::class, $formData);
$formSecond = $this->createForm(ContactFormType::class, $formData);
$form->handleRequest($request);
$formSecond->handleRequest($request);
$sendFlag = false;
$sendFlagSecond = false;
if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData();
$formData->setIpAddress($request->getClientIp());
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
return $this->redirectToRoute('main_page_contact', [
'slugBrand' => $currentBrand->getSlugName(),
]);
} else if ($formSecond->isSubmitted() && $formSecond->isValid()) {
$formData = $formSecond->getData();
$formData->setIpAddress($request->getClientIp());
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
return $this->redirectToRoute('main_page_contact', [
'slugBrand' => $currentBrand->getSlugName(),
]);
}
return $this->render('main_page/widget/contact/contact-form.html.twig', [
'form' => $form->createView(),
'formSecond' => $formSecond->createView(),
'wasSent' => $sendFlag,
'wasSentSecond' => $sendFlagSecond,
]);
}
}
表单是通过Twig的以下代码呈现的:
render(controller('App \ Controller \ MainPage \ ContactController :: renderForm',{'request':app.request,'currentBrand':currentBrand}))}}
感谢答案!
答案 0 :(得分:0)
我有解决办法。 RedirectToRoute应该使用与@Route相同的方法,由redirectToRoute调用。看起来一切都可行。
/**
* @Route("/{_locale}/contact/{slugBrand}/", name="main_page_contact")
*/
class ContactController extends Controller
{
public function index(Request $request, $slugBrand)
{
$currentBrand = $this->getDoctrine()
->getRepository(Brand::class)
->getOneOrNullVisibleBySlugName($slugBrand);
if (is_null($currentBrand)) {
return new Response('Brak marki');
}
$formData = new ContactEmail();
$form = $this->createForm(ContactFormType::class, $formData);
$formSecond = $this->createForm(ContactFormType::class, $formData);
$form->handleRequest($request);
$formSecond->handleRequest($request);
$sendFlag = false;
$sendFlagSecond = false;
if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData();
$formData->setIpAddress($request->getClientIp());
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
return $this->redirectToRoute('main_page_contact', [
'slugBrand' => $currentBrand->getSlugName()]);
} else if ($formSecond->isSubmitted() && $formSecond->isValid()) {
$formData = $formSecond->getData();
$formData->setIpAddress($request->getClientIp());
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
return $this->redirectToRoute('main_page_contact', [
'slugBrand' => $currentBrand->getSlugName()]);
}
return $this->render('main_page/widget/contact/contact-form.html.twig', [
'form' => $form->createView(),
'formSecond' => $formSecond->createView(),
'wasSent' => $sendFlag,
'wasSentSecond' => $sendFlagSecond,
'currentBrand' => $currentBrand,
'slugBrand' => $slugBrand,
'shared' => $this->getAllSharedDataForLayout(),
]);
}