我在我的控制器中创建了一个函数(createUpdate(,它创建并提交了一个有效的表单(数据库被修改(,但重定向给了我一个错误一些强制性参数缺失("title"(,无法生成路由的URL;frContentStrategy">
我想我需要把一些论点传递给:
return $this->redirectToRoute("frContentStrategy");
因为重定向";frContentStrategy;在其路由中使用参数转换器:
@Route("fr/strategy/content/{title}", name="frContentStrategy")
但直到现在,我都找不到让它发挥作用的方法。
这是我的文件,感谢你的帮助:
控制器:
/**
* @Route("fr/strategy/content/{title}", name="frContentStrategy")
*/
public function index(Strategy $strategy): Response
{
return $this->render('content_strategy/contentStrategy.html.twig', [
"strategy" => $strategy
]);
}
/**
* @Route("fr/strategy/content/createforce/{title}", name="frCreateForce")
* @Route("/fr/strategy/content/updateforce/{title}", name="frUpdateForce", methods="GET|POST")
*/
public function createUpdate(DiagnosticForce $diagnosticforce = null, Request $request, EntityManagerInterface $em)
{
if(!$diagnosticforce){
$diagnosticforce = new DiagnosticForce();
}
$form = $this->createForm(DiagnosticForceType::class, $diagnosticforce);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em->persist($diagnosticforce);
$em->flush();
return $this->redirectToRoute("frContentStrategy");
}
return $this->render('content_strategy/createForce.html.twig', [
"diagnosticforce" => $diagnosticforce,
"form" => $form->createView()
]);
}
表单类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('strenght')
->add('strategy', EntityType::class,[
'class' => Strategy::class,
'choice_label' => 'title'
])
;
}
contentStrategy.html.twig:
<section class="newStrat">
<a href="{{path('frStrategy')}}">
<div>Liste des stratégies</div>
</a>
</section>
<section>
<h1>{{strategy.title}}</h1>
<a href="{{path('frCreateForce', {'title' : strategy.title}) }}">
<div>Ajouter une force</div>
</a>
</section>
createForce.html.twig:
{{form_start(form)}}
<div class="divForm1">
<img class="statue" src="{{asset('img/Calque 2.png')}}" alt="image d'accueil visage statue">
<h2>Libellé de la force</h2>
<div class="loginForm">
{{form_row(form.strenght)}}
</div>
<input type="submit" value="Valider">
</div>
{{form_end(form)}}
我终于找到了解决方案。感谢@craigh的帮助和指导。
为了解决这个问题,我不得不将方法createUpdate((更改为:(所有内容都在控制器文件中(
public function createUpdate(Strategy $id, DiagnosticForce $diagnosticforce = null, Request $request, EntityManagerInterface $em)
然后我在重定向中添加了一个参数:
return $this->redirectToRoute("frContentStrategy", [
'title' => $id->getTitle()
]);