, (*1)
, (*2)
OpenGraphBundle
The TenoloOpenGraphBundle is a simple way to improve how you manage OpenGraph
into your Symfony2 application., (*3)
Note: OpenGraph is a standard protocol used by many websites (Facebook,
Twitter, Google, ...) to obtain more precise informations about your content., (*4)
Learn more about OpenGraph, (*5)
The idea of this bundle it to associate each entity of your app with an OpenGraph
map, a service able to create the OpenGraph document for your entity., (*6)
It also works with any other type of data., (*7)
Installation
Installation is very quick:, (*8)
1. Download it with Composer
Add the bundle to your composer.json
file by running:, (*9)
composer require tenolo/open-graph-bundle
, (*10)
2. Enable it in your kernel
Enable the bundle in your app/AppKernel.php
file;, (*11)
``` php
<?php
// app/AppKernel.php, (*12)
public function registerBundles()
{
$bundles = array(
// ...
new Tenolo\Bundle\OpenGraphBundle\TenoloOpenGraphBundle(),
);
}, (*13)
Usage
-----
The TenoloOpenGraphBundle will associate:
- **Entities** of your application with ...
- ... or **Other Data like an array** of your application with ...
- **OpenGrap maps**, definitions of these entities in an OpenGraph way
Let's take an example for a better understanding: a blog post.
### Your entity
For a blog post, you could have an entity like this one:
``` php
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity
*/
class BlogPost
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
}
Its OpenGraph map
The map associated with your entity will be a class implementing
Tenolo\Bundle\OpenGraphBundle\Map\OpenGraphMapInterface
and the two required methods of this interface :
map(DocumentWriterInterface $document, $data)
and supports($data)
., (*14)
For instance, our map could look like this :, (*15)
``` php
<?php, (*16)
namespace Acme\DemoBundle\OpenGraph;, (*17)
use Acme\DemoBundle\Entity\BlogPost;
use Tenolo\Bundle\OpenGraphBundle\OpenGraph\DocumentWriterInterface;
use Tenolo\Bundle\OpenGraphBundle\Map\OpenGraphMapInterface;
use Opengraph\Opengraph;, (*18)
class BlogPostMap implements OpenGraphMapInterface
{, (*19)
/**
* @inheritdoc
* @var BlogPost $data
*/
public function map(DocumentWriterInterface $document, $data)
{
$document->append(OpenGraph::OG_SITE_NAME, 'MyBlog');
$document->append(OpenGraph::OG_TYPE, OpenGraph::TYPE_ARTICLE);
$document->append(OpenGraph::OG_TITLE, $data->getTitle());
}
/**
* @inheritdoc
*/
public function supports($data)
{
return $entity instanceof BlogPost;
}
}, (*20)
The `supports` method declares with what kind of entities this map is able to deal.
The `map` method create an OpenGraph document representing the given entity.
Once created, we still have to register our class into the OpenGraph manager. To do so,
we will have to use the tag `tenolo_open_graph.map`:
``` yml
services:
acme_demo.open_graph.blog_post_map:
class: Acme\DemoBundle\OpenGraph\BlogPostMap
tags:
- { name: tenolo_open_graph.map }
Using the map
Our map is registered, so we can use it anywhere we want to render it.
For instance, with Twig:, (*21)
``` html
, (*22)
Blog post
{{ opengraph_render(blogPost) }} <!-- blogPost should be an instance of BlogPost -->
</head>
<body>
...
</body>
```, (*23)
Note: if no map is able to deal with the entity given in opengraph_render
,
an NotSupported
exception will be thrown., (*24)
Another Note: Credits and inspiration goes to tgalopin, (*25)
just saying ;), (*26)