src/PHPDish/Bundle/PostBundle/Controller/CategoryController.php
public function createAction(Request $request)
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
        $manager = $this->getCategoryManager();

        $category = $manager->createCategory($this->getUser());
        $form = $this->createForm(CategoryType::class, $category);
        $form->handleRequest($request);

        /**@var TranslatorInterface*/
        $translator = $this->get('translator');
        if (($number = $manager->getUserCategoriesNumber($this->getUser())) >= 2) {
            $this->addFlash('danger', $translator->trans('category.more_than_2_categories', [
                '%count%' => $number
            ]));
        }

        if ($form->isSubmitted() && $form->isValid()) {

            //触发专栏创建事件
            $event = new CategoryCreateEvent($category);
            $this->get('event_dispatcher')->dispatch(Events::CATEGORY_PRE_CREATED, $event);

            if ($event->isCreationAborted()) {
                $this->addFlash('success', $translator->trans('category.add_error'));
            } else {
                $manager->saveCategory($category);
                $this->addFlash('success', $translator->trans('category.add_success'));

                return $this->redirectToRoute('category_view', [
                    'slug' => $category->getSlug(),
                ]);
            }
        }

        return $this->render($this->configuration->getTemplate('Category:create.html.twig'), [
            'form' => $form->createView(),
            'hasManyCategories' => $number >= 2,
            'isBook' => false
        ]);
    }
public function createAction(Request $request)
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
        $manager = $this->getCategoryManager();

        $category = $manager->createCategory($this->getUser());
        $form = $this->createForm(CategoryType::class, $category);
        $form->handleRequest($request);

        /**@var TranslatorInterface*/
        $translator = $this->get('translator');
        if (($number = $manager->getUserCategoriesNumber($this->getUser())) >= 2) {
            $this->addFlash('danger', $translator->trans('category.more_than_2_categories', [
                '%count%' => $number
            ]));
        }

        if ($form->isSubmitted() && $form->isValid()) {

            //触发专栏创建事件
            $event = new CategoryCreateEvent($category);
            $this->get('event_dispatcher')->dispatch(Events::CATEGORY_PRE_CREATED, $event);

            if ($event->isCreationAborted()) {
                $this->addFlash('success', $translator->trans('category.add_error'));
            } else {
                $manager->saveCategory($category);
                $this->addFlash('success', $translator->trans('category.add_success'));

                return $this->redirectToRoute('category_view', [
                    'slug' => $category->getSlug(),
                ]);
            }
        }

        return $this->render($this->configuration->getTemplate('Category:create.html.twig'), [
            'form' => $form->createView(),
            'hasManyCategories' => $number >= 2,
            'isBook' => false
        ]);
    }
@Route("/categories/new", name="category_add")

@param Request $request

@return Response