src/PHPDish/Bundle/PostBundle/Controller/PostController.php
public function viewAction($id, Request $request)
    {
        $post = $this->getPostManager()->findPostById($id);
        if (!$post->isEnabled()) {
            throw $this->createNotFoundException();
        }
        //如果是电子书则跳转到阅读页面
        if ($post->getCategory()->isBook()) {
            return $this->redirectToRoute('book_read', [
                'slug' => $post->getCategory()->getSlug(),
                'chapterId' => $post->getId()
            ], 301);
        }
        $form = $this->createForm(CommentType::class);
        $criteria = Criteria::create()->where(Criteria::expr()->eq('post', $post->getId()));
        $comments = $this->getPostCommentManager()->findCommentsPager($criteria, $request->query->getInt('page', 1));

        $this->getPostManager()->increasePostViews($post);

        //SEO
        $seoPage = $this->get('sonata.seo.page');
        $summary = StringManipulator::stripLineBreak($post->getSummary());
        $seoPage
            ->setTitle($post->getTitle())
            ->removeMeta('name', 'keywords')
            ->addMeta('name', 'description', $summary)
            ->addMeta('property', 'og:title', $post->getTitle())
            ->addMeta('property', 'og:type', 'article')
            ->addMeta('property', 'og:url',  $this->generateUrl('post_view', ['id' => $post->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
            ->addMeta('property', 'og:description', $summary);

        return $this->render($this->configuration->getTemplate('Post:view.html.twig'), [
            'post' => $post,
            'comments' => $comments,
            'form' => $form->createView(),
        ]);
    }
public function viewAction($id, Request $request)
    {
        $post = $this->getPostManager()->findPostById($id);
        if (!$post->isEnabled()) {
            throw $this->createNotFoundException();
        }
        //如果是电子书则跳转到阅读页面
        if ($post->getCategory()->isBook()) {
            return $this->redirectToRoute('book_read', [
                'slug' => $post->getCategory()->getSlug(),
                'chapterId' => $post->getId()
            ], 301);
        }
        $form = $this->createForm(CommentType::class);
        $criteria = Criteria::create()->where(Criteria::expr()->eq('post', $post->getId()));
        $comments = $this->getPostCommentManager()->findCommentsPager($criteria, $request->query->getInt('page', 1));

        $this->getPostManager()->increasePostViews($post);

        //SEO
        $seoPage = $this->get('sonata.seo.page');
        $summary = StringManipulator::stripLineBreak($post->getSummary());
        $seoPage
            ->setTitle($post->getTitle())
            ->removeMeta('name', 'keywords')
            ->addMeta('name', 'description', $summary)
            ->addMeta('property', 'og:title', $post->getTitle())
            ->addMeta('property', 'og:type', 'article')
            ->addMeta('property', 'og:url',  $this->generateUrl('post_view', ['id' => $post->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
            ->addMeta('property', 'og:description', $summary);

        return $this->render($this->configuration->getTemplate('Post:view.html.twig'), [
            'post' => $post,
            'comments' => $comments,
            'form' => $form->createView(),
        ]);
    }
查看文章.

@Route("/posts/{id}", name="post_view", requirements={"id": "\d+"}, methods={"GET"})

@param int    $id
@param Request $request

@return Response