Symfony处理表单请求逻辑

时间:2018-06-19 07:44:16

标签: symfony

很抱歉,如果这个问题太乱,但在Symfony中有一种处理表单请求的常用方法吗? (我使用SF 4)。

现在,我的控制器中有逻辑:

$formBooking = $this->createForm(BookingType::class);

    $formBooking->handleRequest($request);

    if ($formBooking->isSubmitted() && $formBooking->isValid()) {
        // perform actions ...

我在同一页面上有几个表单,所以我的控制器变得越来越大。

我想创建一个新的文件夹,例如操作,并将逻辑放在这里。

在我的控制器中做:

 $formBooking = $this->createForm(BookingType::class);

 // $bookingAction = new App\Action\BookingAction
 $bookingAction->handleRequest($formBooking, $request);

我只是想知道是否有任何"官方"这样的方式?

1 个答案:

答案 0 :(得分:0)

您可以创建一个抽象的“BaseController”,并继承您的其他控制器。我不认为这是一种“官方”方式,但似乎是正确的。

在我的示例中,我已将实体管理器,表单工厂和路由设置为服务。此外,它被称为BaseManager,因为我的所有逻辑都在管理器文件中,而我的控制器没有代码(只是调用我的经理,除了特定情况)

要做到这一点,只需参考您的服务,让您的控制器成为主人,但是干净而简单。

## Controller##
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Category;
use AppBundle\Form\CategoryType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
/**
 * @Route("/admin/category")
 */
class CategoryController extends Controller
{
    /**
     * @Template()
     * @Route("/", name="category_index")
     * @return array
     */
    public function indexAction()
    {
        $categories = $this->get('app.category.manager')->find();
        return array('categories' => $categories);
    }
    /**
     * @Template()
     * @Route("/", name="category_menu")
     * @return array
     */
    public function menuAction()
    {
        $categories = $this->getDoctrine()->getRepository('AppBundle:Category')->findAllOrdered("ASC");
        return array('categories' => $categories);
    }
    /**
     * @Template()
     * @Route("/icones/glyficones", name="category_icones_glyficones")
     * @return array
     */
    public function fontawesomeAction()
    {
        return array();
    }
    /**
     * @Template()
     * @Route("/icones/fontawesome", name="category_icones_fontawesome")
     * @return array
     */
    public function glyficoneAction()
    {
        return array();
    }
    /**
     * @Template()
     * @Route("/new", name="category_create")
     * @param Request $request
     * @return array
     */
    public function newAction(Request $request)
    {
        return $this->get('app.category.manager')->save($request);
    }
    /**
     * @Template()
     * @Route("/{id}/edit", name="category_edit")
     * @param Request $request
     * @param $id
     * @return array
     */
    public function editAction(Request $request, $id)
    {
        return $this->get('app.category.manager')->edit($request, $id);
    }
    /**
     * @Template()
     * @Route("/{id}/delete", name="category_delete")
     * @param Request $request
     * @param $id
     * @return array
     */
    public function deleteAction(Request $request, $id)
    {
        return $this->get('app.category.manager')->delete($request, $id);
    }
}

基本经理

<?php
namespace AppBundle\Manager;
use AppBundle\Form\CategoryType;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
abstract class BaseManager
{
    protected $em;
    protected $formFactory;
    protected $router;
    /**
     * CategoryManager constructor.
     * @param $em
     * @param $formFactory
     * @param Router $router
     */
    public function __construct($em, $formFactory, Router $router)
    {
        $this->em = $em;
        $this->formFactory = $formFactory;
        $this->router = $router;
    }
    /**
     * @param $entity
     */
    protected function persistAndFlush($entity)
    {
        $this->em->persist($entity);
        $this->em->flush();
    }
    /**
     * @param $entity
     */
    protected function removeAndFlush($entity)
    {
        $this->em->remove($entity);
        $this->em->flush();
    }
    /**
     * @param Request $request
     * @param Form $form
     * @param $entity
     * @param $path
     * @return array|RedirectResponse
     */
    protected function handleBaseForm(Request $request, Form $form, $entity, $path)
    {
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $this->persistAndFlush($entity);
            return new RedirectResponse($this->router->generate($path));
        }
        return array('form' => $form->createView());
    }
    /**
     * @param $route
     * @return RedirectResponse
     */
    protected function redirect($route)
    {
        return new RedirectResponse($this->router->generate($route));
    }
}

这里有一个例子,一个简单的“类别”实体。

特定实体经理

<?php
namespace AppBundle\Manager;
use AppBundle\Entity\Category;
use AppBundle\Form\CategoryType;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
 * Class CategoryManager
 * @package AppBundle\Manager
 */
class CategoryManager extends BaseManager
{
    /**
     * CategoryManager constructor.
     * @param $em
     * @param $formFactory
     * @param Router $router
     */
    public function __construct($em, $formFactory, Router $router)
    {
        parent::__construct($em, $formFactory, $router);
    }
    /**
     * @return mixed
     */
    public function find()
    {
        return $this->em->getRepository('AppBundle:Category')->findAll();
    }
    /**
     * @param Request $request
     * @return array
     */
    public function save(Request $request)
    {
        $category = new Category();
        return $this->handleForm($request, $category);
    }
    /**
     * @param Request $request
     * @param $id
     * @return array|RedirectResponse
     */
    public function edit(Request $request, $id)
    {
        $category = $this->em->getRepository('AppBundle:Category')->find($id);
        return $this->handleForm($request, $category);
    }
    /**
     * @param Request $request
     * @param $id
     * @return RedirectResponse
     */
    public function delete(Request $request, $id)
    {
        $category = $this->em->getRepository('AppBundle:Category')->find($id);
        $this->persistAndFlush($category);
        return $this->redirect('category_index');
    }
    /**
     * @param Request $request
     * @param Category $category
     * @return array|RedirectResponse
     */
    public function handleForm(Request $request, $category)
    {
        $form = $this->formFactory->create(CategoryType::class, $category);
        return $this->handleBaseForm($request, $form, $category, "category_index");
    }
}

如果需要,这是我的services.yaml文件

services:
  app.category.manager:
    class: AppBundle\Manager\CategoryManager
    arguments: [ '@doctrine.orm.entity_manager', '@form.factory', '@router' ]

  fos_user.doctrine_registry:
      alias: doctrine

希望它可能有助于解决您的问题,并获得更清晰的代码。请注意,这是一个个人解决方案,可能有更好的解决方案。但这个是一个工作的,对我来说,似乎更清洁,而不仅仅是让控制器拥有所有的逻辑