Class that assists in building Open Graph meta tags.
Add chriskonnertz/open-graph
to composer.json
:
"chriskonnertz/open-graph": "dev-master"
Run composer update
to get the latest version of Open Graph Builder.
In Laravel 4 you may add an alias to app/config/app.php
:
'aliases' => array(
// ...
'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraph',
),
In Laravel 5 the path to this file is
config/app.php
.
There is also a service provider and a facade. Add the service provider to the config file:
'providers' => array(
// ...
'ChrisKonnertz\OpenGraph\OpenGraphServiceProvider',
),
To create an alias for the facade, add a new entry (or replace the one created before):
'aliases' => array(
// ...
'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraphFacade',
),
If you need to reset the underlying instance call
OpenGraph::clear()
.
Example:
$og = new OpenGraph();
$og->title('Apple Cookie')
->type('article')
->image('http://example.org/apple.jpg')
->description('Welcome to the best apple cookie recipe never created.')
->url();
Render these tags in a template as follows:
{{ $og->renderTags() }}
Providing Open Graph tags enriches web pages. The downside is some extra time to spend, because every model has its own way to generate these tags. It's also important to follow the official protocol. Read the documentation to learn more about the tags that are available and the values they support or check out examples. Please note that this implementation sticks to the specification of OGP.me and does not support the enhancements created by Facebook.
$og->title('Apple Cookie')
->type('article')
->description('A delicious recipe')
->url()
->locale('en_US')
->localeAlternate(['en_UK'])
->siteName('Cookie Recipes Website')
->determiner('an');
If no argument is passed to the
url
method the current URL is applied.
Note that DateTime
objects will be converted to ISO 8601 strings.
You may add image
, audio
or video
tags and pass the basic value (the URL to the object) and an array of additional attributes.
$og->image($imageUrl, [
'width' => 300,
'height' => 200
]);
$og->audio($audioUrl, [
'type' => 'audio/mpeg'
]);
$og->video($videoUrl, [
'width' => 300,
'height' => 200,
'type' => 'application/x-shockwave-flash'
]);
Some object types (determined by the type
tag) have their own tags with attributes but not a basic tag. These are article
, book
and profile
.
$og->article([
'author' => 'Jane Doe'
]);
$og->book([
'author' => 'John Doe'
]);
$og->profile([
'first_name' => 'Kim'
'last_name' => 'Doe'
]);
Facebook supports more than just the basic object types. To add attributes for off-the-record object types you may use the attributes
method.
Without custom validation rule:
$og->attributes('product', ['product:color' => 'red']);
With custom validation rule:
$og->attributes('product', ['product:color' => 'red'], ['product:color']);
The only validation this method performs is to check if all attribute names match with the list of attribute names.
A property can have multiple values. Add the tag several times to achieve this effect.
$og->image('http://example.org/apple.jpg')
->image('http://example.org/tree.jpg');
Adding a basic tag a second time will override the value of the first tag. Basic tags must not exist several times.
If validation is enabled (default is disabled) adding tags will trigger validation. Validation is not covering the complete specification but some important parts. If validation fails the method will throw an exception.
Validation checks if tag values are legit and if attribute types are known.
Enable validation by method:
$og->validate();
By constructor:
$og = new OpenGraph(true);
Disable validation:
$og->validate(false);
$hasTitle = $og->has('title');
$og->forget('title');
$og->clear();
$og->tag('apples', 7);
$tag = $og->lastTag('image');
$value = $tag['value'];
Tags are stored as arrays consisting of name-value-pairs.