abdellahramadan/open-graph-bundle
Symfony bundle for generating Open Graph meta tags. Install via Composer, enable the bundle, then use provided docs and examples to add OG tags to your pages for better social sharing previews.
Installation:
composer require abdellahramadan/open-graph-bundle
For non-Flex projects, also register the bundle in config/bundles.php:
Abdellahramadan\OpenGraph\OpenGraphBundle::class => ['all' => true],
First Use Case:
Inject the OpenGraphService into a controller or service:
use Abdellahramadan\OpenGraph\Service\OpenGraphService;
class MyController extends AbstractController
{
public function __construct(private OpenGraphService $openGraph)
{
}
public function show(Post $post)
{
$this->openGraph->setTitle($post->title)
->setDescription($post->excerpt)
->setImage($post->featuredImageUrl)
->setUrl(route('posts.show', $post));
return $this->render('post/show.html.twig');
}
}
Where to Look First:
<head> via Twig’s open_graph block.config/packages/abdellahramadan_open_graph.yaml for defaults (e.g., site name, default image).Dynamic Metadata: Use the fluent interface in controllers/services to set per-page metadata:
$this->openGraph
->setType('article')
->setAuthor('Jane Doe')
->addProperty('article:published_time', $post->publishedAt->format('c'));
Reusable Services: Create a dedicated service to encapsulate Open Graph logic:
class PostOpenGraphService
{
public function __construct(private OpenGraphService $openGraph) {}
public function generateFor(Post $post): void
{
$this->openGraph->reset()
->setTitle($post->title)
->setImage($post->getOgImageUrl())
->setUrl(route('posts.show', $post));
}
}
Twig Integration: Override defaults in templates:
{% block open_graph %}
{{ parent() }}
<meta property="og:locale" content="en_US" />
{% endblock %}
Event-Driven Updates:
Listen to kernel events (e.g., KernelEvents::VIEW) to auto-generate metadata:
public function onKernelView(ViewEvent $event): void
{
$post = $event->getControllerResult();
if ($post instanceof Post) {
$this->openGraph->generateFor($post);
}
}
API Responses:
Use the OpenGraphService to generate metadata for API responses (e.g., JSON-LD):
return response()->json([
'data' => $post,
'meta' => $this->openGraph->toArray(), // Converts to associative array
]);
Caching Headers:
Cache-Control headers for dynamic pages.Cache-Control: no-cache to responses where metadata changes frequently.Image Validation:
setImage() method doesn’t validate URLs. Broken images can break OG rendering.if (!filter_var($post->featuredImageUrl, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('Invalid image URL');
}
Twig Block Conflicts:
open_graph block may break if the parent template doesn’t call parent().{{ parent() }} to preserve default tags.Type-Specific Properties:
og:type (e.g., website, article) can cause social media parsers to ignore metadata.abdellahramadan_open_graph:
default_type: 'article'
URL Canonicalization:
$this->openGraph->setUrl(url()->current()); // Symfony helper
public function onKernelRequest(RequestEvent $event): void
{
$tags = $this->openGraph->toArray();
if (empty($tags['og:title'])) {
$this->logger->warning('Missing OG title');
}
}
Custom Properties: Extend the service to support custom OG properties:
$this->openGraph->addProperty('custom:key', 'value');
Multiple Contexts: Use dependency injection to manage separate instances (e.g., for admin vs. public):
services:
app.open_graph.admin:
class: Abdellahramadan\OpenGraph\Service\OpenGraphService
tags: ['abdellahramadan_open_graph.service']
Event Dispatching: Dispatch events when metadata is generated (e.g., for analytics):
$this->openGraph->onGenerate(function (array $tags) {
$this->eventDispatcher->dispatch(new OpenGraphGeneratedEvent($tags));
});
Fallback Values:
Configure fallbacks in config/packages/abdellahramadan_open_graph.yaml:
abdellahramadan_open_graph:
default:
title: 'Default Title'
image: '%kernel.project_dir%/public/images/default-og.png'
How can I help you explore Laravel packages today?