noith / opengraph
A lightweight Laravel package for managing Open Graph meta tags.
Requires
- php: ^8.2
- laravel/framework: ^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^10.8
- phpunit/phpunit: ^12.5
This package is auto-updated.
Last update: 2026-08-06 18:28:08 UTC
README
A lightweight Laravel package for collecting and rendering Open Graph meta tags. It provides a facade, a global helper, and a Blade component.
Requirements
- PHP 8.2 or later
- Laravel 12 or 13
Installation
Install the package with Composer:
composer require noith/opengraph
Laravel package discovery registers the service provider automatically.
Usage
Set the tags in a controller, middleware, or view. The fluent facade API is the most convenient option:
use Noith\Opengraph\Facades\OpenGraph;
OpenGraph::title('My page title')
->description('A concise description of this page.')
->type('website')
->url('https://example.com/articles/hello')
->image('https://example.com/images/hello.jpg')
->siteName('Example');
The og() helper is an alternative for setting individual tags:
og('title', 'My page title');
og('image', 'https://example.com/images/hello.jpg');
Render the tags once inside the document <head>:
<x-opengraph::opengraph />
This produces markup such as:
<meta property="og:title" content="My page title">
<meta property="og:type" content="website">
<meta property="og:url" content="https://example.com/articles/hello">
<meta property="og:image" content="https://example.com/images/hello.jpg">
Defaults
Pass defaults to the Blade component. A default is used only when that tag was not already set:
<x-opengraph::opengraph
default-title="Example"
default-type="website"
default-url="https://example.com"
default-image="https://example.com/images/default.jpg"
default-description="Default page description"
default-site-name="Example"
/>
Supported defaults are title, description, type, image, url, audio, locale, site-name, video, and determiner.
Multiple values
image, audio, video, and locale accept multiple values. Call the method more than once or pass an array:
OpenGraph::image([
'https://example.com/images/cover.jpg',
'https://example.com/images/preview.jpg',
]);
og('locale', 'en_US');
og('locale', 'ru_RU');
For structured media values, use an associative array. Its keys become Open Graph sub-properties:
OpenGraph::image([
'url' => 'https://example.com/images/cover.jpg',
'width' => '1200',
'height' => '630',
]);
Validation and rendering control
By default, rendering validates that title, type, image, and url are present, and throws an InvalidArgumentException if one is missing. Disable strict validation when partial tag sets are acceptable:
use Noith\Opengraph\OpenGraphManager;
OpenGraphManager::$strict = false;
Tag values have whitespace normalized by default. Preserve original whitespace if needed:
OpenGraphManager::$squish = false;
You can temporarily stop output or reset the current set of tags:
OpenGraph::disable();
OpenGraph::enable();
OpenGraph::clear();
Available tags
title, description, type, image, url, audio, locale, site_name, video, and determiner are supported. You may use the OpenGraphTypes enum instead of strings.
Testing
composer test
License
This package is open-sourced software licensed under the MIT license.