Skip to content

Instantly share code, notes, and snippets.

@rande
Created February 19, 2012 21:25
Show Gist options
  • Save rande/1865874 to your computer and use it in GitHub Desktop.
Save rande/1865874 to your computer and use it in GitHub Desktop.
PostController with SEO Information
<?php
class PostController extends Controller
{
/**
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @param $permalink
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response
*/
public function viewAction($permalink)
{
$post = $this->getPostManager()->findOneByPermalink($permalink, $this->container->get('sonata.news.blog'));
if (!$post || !$post->isPublic()) {
throw new NotFoundHttpException('Unable to find the post');
}
if ($seoPage = $this->getSeoPage()) {
$seoPage
->setTitle($post->getTitle())
->addMeta('property', 'og:title', $post->getTitle())
->addMeta('property', 'og:type', 'blog')
->addMeta('property', 'og:url', $this->generateUrl('sonata_news_view', array(
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post, true)
), true))
->addMeta('property', 'og:description', $post->getAbstract())
;
}
return $this->render('SonataNewsBundle:Post:view.html.twig', array(
'post' => $post,
'form' => false,
'blog' => $this->get('sonata.news.blog')
));
}
/**
* @return \Sonata\SeoBundle\Seo\SeoPageInterface
*/
public function getSeoPage()
{
if ($this->has('sonata.seo.page')) {
return $this->get('sonata.seo.page');
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment