pontedilana / open-graph-bundle
A Symfony OpenGraph bundle
Installs: 3 200
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 7
Type:symfony-bundle
Requires
- php: 7.4.* || 8.1.* || 8.2.*
- euskadi31/opengraph: ^1.0 || ^2.0
- symfony/config: ^5.4 || ^6.2
- symfony/dependency-injection: ^5.4 || ^6.2
- symfony/framework-bundle: ^5.4 || ^6.2
- symfony/http-kernel: ^5.4 || ^6.2
- symfony/twig-bundle: ^5.4 || ^6.2
- twig/twig: ^3.5
Requires (Dev)
- matthiasnoback/symfony-config-test: ^4.3
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- symfony/phpunit-bridge: ^5.4 || ^6.2
This package is auto-updated.
Last update: 2024-10-30 12:54:12 UTC
README
OpenGraphBundle
The PontedilanaOpenGraphBundle is a simple way to improve how you manage OpenGraph into your Symfony application, through the use of the euskadi31/opengraph library.
This repository is a fork of tenolo/open-graph-bundle maintained by Pontedilana; support for PHP 8, Symfony 5 and 6 as been added.
Note: OpenGraph is a standard protocol used by many websites (Facebook, Twitter, Google, ...) to obtain more precise information about your content.
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.
It also works with any other type of data.
Installation
Installation is very quick:
1. Download it with Composer
Add the bundle to your composer.json
file by running:
composer require pontedilana/open-graph-bundle
2. Enable it in your kernel
Enable the bundle in your app/AppKernel.php
file;
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Pontedilana\OpenGraphBundle\PontedilanaOpenGraphBundle(), ); }
Usage
The PontedilanaOpenGraphBundle 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 namespace App\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
Pontedilana\OpenGraphBundle\Map\OpenGraphMapInterface
and the two required methods of this interface :
map(DocumentWriterInterface $document, $data)
and supports($data)
.
For instance, our map could look like this :
<?php namespace App\OpenGraph; use App\Entity\BlogPost; use Pontedilana\OpenGraphBundle\OpenGraph\DocumentWriterInterface; use Pontedilana\OpenGraphBundle\Map\OpenGraphMapInterface; use Opengraph\Opengraph; class BlogPostMap implements OpenGraphMapInterface { /** * @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()); } public function supports($data) { return $entity instanceof BlogPost; } }
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 pontedilana_open_graph.map
:
services: App\OpenGraph\: resource: '../src/OpenGraph' tags: [ 'pontedilana_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:
<html> <head> <title>Blog post</title> {{ opengraph_render(blogPost) }} <!-- blogPost should be an instance of BlogPost --> </head> <body> ... </body> </html>
Note: if no map is able to deal with the entity given in
opengraph_render
, anNotSupported
exception will be thrown.
Another Note: Credits and inspiration goes to tgalopin