Php Symfony3新建和编辑控制器逻辑
在管理后端中,我希望提供以下功能: 用户应该能够使用简单的表单创建配方 用户可以查看表单中直接呈现的现有配方,因此可以进行更改并保存表单 我认为这是一项微不足道的任务。我创建了实体和表单类型,但对于控制器应该如何工作,我完全是空白 这是我的控制器:Php Symfony3新建和编辑控制器逻辑,php,symfony,Php,Symfony,在管理后端中,我希望提供以下功能: 用户应该能够使用简单的表单创建配方 用户可以查看表单中直接呈现的现有配方,因此可以进行更改并保存表单 我认为这是一项微不足道的任务。我创建了实体和表单类型,但对于控制器应该如何工作,我完全是空白 这是我的控制器: <?php declare(strict_types=1); namespace App\Controller; // use statements... class RecipeController extends Controller
<?php
declare(strict_types=1);
namespace App\Controller;
// use statements...
class RecipeController extends Controller
{
/**
* @Route("/admin/recipes", name="recipe_index")
* @Method("GET")
*/
public function indexAction(Request $request) : Response
{
// code to fetch paginated list of recipes
// and render it
}
/**
* @Route("/admin/recipes/new", name="recipe_new")
* @Method("POST")
*/
public function newAction(Request $request) : Response
{
$form = $this->createForm(RecipeType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$recipe = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($recipe);
$entityManager->flush();
$this->addFlash(
'success',
'Recipe successfully added!');
return $this->redirectToRoute('recipe_index');
}
return $this->render('Admin/recipe_form.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/admin/recipes/{id}", name="recipe_detail")
* @Method({"GET"})
*/
public function editAction(Request $request, Recipe $recipe) : Response
{
$form = $this->createForm(RecipeType::class,$recipe);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$recipe = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($recipe);
$entityManager->flush();
$this->addFlash(
'success',
'Recipe successfully updated!');
return $this->redirectToRoute('recipe_index');
}
return $this->render('Admin/recipe_form_edit.html.twig', [
'form' => $form->createView(),
'recipe' => $recipe
]);
}
}
这种方法存在几个问题:
create和update方法中的代码重复
对应的细枝模板中的代码重复
由于create和update表单没有区别,我想知道如何重用代码 我纠正了代码中的一些问题:更新表单时不需要持久化,编辑表单时不需要添加对象,表单已经有了数据。可以对两个视图使用相同的模板和表单
class RecipeController extends Controller
{
/**
* @Route("/admin/recipes", name="recipe_index")
* @Method("GET")
*/
public function indexAction(Request $request) : Response
{
// code to fetch paginated list of recipes
// and render it
}
/**
* @Route("/admin/recipes/new", name="recipe_new")
* @Method("POST")
*/
public function newAction(Request $request)
{
$recipe = new Recipe();
$form = $this->createForm(RecipeType::class, $recipe);
if ($form->isSubmitted() && $form->handleRequest($request)->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($recipe);
$entityManager->flush();
$this->addFlash(
'success',
'Recipe successfully added!');
return $this->redirectToRoute('recipe_index');
}
return $this->render('Admin/recipe_form.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/admin/recipes/{id}", name="recipe_detail")
* @Method({"GET"})
*/
public function editAction(Request $request, Recipe $recipe) : Response
{
$form = $this->createForm(RecipeType::class,$recipe);
if ($form->isSubmitted() && $form->handleRequest($request)->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
// No need to persist, the object is already persisted, just flush
$entityManager->flush();
$this->addFlash(
'success',
'Recipe successfully updated!');
return $this->redirectToRoute('recipe_index');
}
// You don't need to use the recipe_form_edit, you can use the one you created above
return $this->render('Admin/recipe_form.html.twig', [
// Form already has its values filled-in no need to add the entity
'form' => $form->createView(),
]);
}
}