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

        /**@var TranslatorInterface*/
        $translator = $this->get('translator');
        //如果用户没有专栏，则跳转专栏创建页面
        if ($this->getCategoryManager()->getUserCategoriesNumber($this->getUser()) === 0) {
            $this->addFlash('warning', $translator->trans('post.add_category_first'));
            return $this->redirectToRoute('category_add');
        }

        $manager = $this->getPostManager();
        $post = $manager->createPost($this->getUser());
        $form = $this->createForm(PostType::class, $post, [
            'user' => $this->getUser(),
        ]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            if ($manager->savePost($post)) {
                //触发事件
                $this->get('event_dispatcher')->dispatch(Events::POST_CREATED, new PostEvent($post));

                return $this->redirectToRoute('post_view', [
                    'id' => $post->getId(),
                ]);
            } else {
                $this->addFlash('error', $translator->trans('post.cannot_add'));
            }
        }

        return $this->render($this->configuration->getTemplate('Post:create.html.twig'), [
            'form' => $form->createView(),
        ]);
    }
public function createAction(Request $request)
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');

        /**@var TranslatorInterface*/
        $translator = $this->get('translator');
        //如果用户没有专栏，则跳转专栏创建页面
        if ($this->getCategoryManager()->getUserCategoriesNumber($this->getUser()) === 0) {
            $this->addFlash('warning', $translator->trans('post.add_category_first'));
            return $this->redirectToRoute('category_add');
        }

        $manager = $this->getPostManager();
        $post = $manager->createPost($this->getUser());
        $form = $this->createForm(PostType::class, $post, [
            'user' => $this->getUser(),
        ]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            if ($manager->savePost($post)) {
                //触发事件
                $this->get('event_dispatcher')->dispatch(Events::POST_CREATED, new PostEvent($post));

                return $this->redirectToRoute('post_view', [
                    'id' => $post->getId(),
                ]);
            } else {
                $this->addFlash('error', $translator->trans('post.cannot_add'));
            }
        }

        return $this->render($this->configuration->getTemplate('Post:create.html.twig'), [
            'form' => $form->createView(),
        ]);
    }
创建文章.

@Route("/posts/new", name="post_add")

@param Request $request

@return Response