2017 © Pedro Peláez
 

library open-graph

Class that assists in building Open Graph meta tags

image

chriskonnertz/open-graph

Class that assists in building Open Graph meta tags

  • Sunday, March 11, 2018
  • by siconize
  • Repository
  • 3 Watchers
  • 80 Stars
  • 44,440 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 2 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

Open Graph Builder

Build Status Monthly Downloads Version GitHub license, (*1)

Library that assists in building Open Graph meta tags., (*2)

Installation

Add chriskonnertz/open-graph to composer.json with a text editor:, (*3)

"chriskonnertz/open-graph": "~2"

Or via a console:, (*4)

composer require chriskonnertz/open-graph

In the future use composer update to update to the latest version of Open Graph Builder., (*5)

This library requires PHP >=7.0., (*6)

Framework Support

Laravel >=5.5 can auto-detect this package so you can ignore this section. In Laravel 5.0-5.4 you have to edit your config/app.php config file. You can either add an alias to the object so you can create a new instance via new OpenGraph() ..., (*7)

'aliases' => [
    ...
    'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraph',
],

...or an alias to the facade (this is what happens in Laravel >=5.5 via package auto-discovery) so you do not have to create the instance by yourself but you can access it via pseudo-static methods. If you choose this path you also have to add the service provider to the config file:, (*8)

'aliases' => [
    ...
    'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraphFacade',
],

...

'providers' => [
    ...
    'ChrisKonnertz\OpenGraph\OpenGraphServiceProvider',
],

If you need to reset the underlying instance of the facade (the OpenGraph object), call OpenGraph::clear()., (*9)

Introduction

Example:, (*10)

$og = OpenGraph::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:, (*11)

{!! $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., (*12)

Add Tags And Attributes

Add Basic Tags

$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 the environment variable APP_URL is considered if it is set. Furthermore, when executed via CLI, and APP_URL is not set, the domain will be localhost., (*13)

Note that DateTime objects will be converted to ISO 8601 strings., (*14)

Add Tags With Attributes

You may add image, audio or video tags and pass the basic value (the URL to the object) and an array of additional attributes., (*15)

$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'
]);

Add Type Attributes

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., (*16)

$og->article([
    'author'        => 'Jane Doe'
]);

$og->book([
    'author'        => 'John Doe'
]);

$og->profile([
    'first_name'    => 'Kim',
    'last_name'     => 'Doe'
]);

Add Attributes

Facebook supports more than just the basic object types. To add attributes for off-the-record object types you may use the attributes method., (*17)

Without custom validation rule:, (*18)

$og->attributes('product', ['product:color' => 'red']);

With custom validation rule:, (*19)

$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., (*20)

Add A Tag Several Times

A property can have multiple values. Add the tag several times to achieve this effect., (*21)

$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., (*22)

Validation

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., (*23)

Validation checks if tag values are legit and if attribute types are known., (*24)

Enable validation by method:, (*25)

$og->validate();

By constructor:, (*26)

$og = new OpenGraph(true);

Disable validation:, (*27)

$og->validate(false);

Miscellaneous

Determine If A Tag Exists

$hasTitle = $og->has('title');

Remove A Tag From The List

$og->forget('title');

Remove All Tags From The List

$og->clear();

Add A Custom Tag

$og->tag('apples', 7);

To disable auto-prefixing pass a third parameter: $og->tag('apples', 7, false), (*28)

Get The Last Tag (By Name)

$tag = $og->lastTag('image');
$value = $tag['value'];

Tags are stored as arrays consisting of name-value-pairs., (*29)

Status

Status of this repository: Maintained. If you create an issue you will get a response usually within 48 hours., (*30)

The Versions

11/03 2018

dev-master

9999999-dev

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph ogp

17/09 2017

v1.0.5

1.0.5.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

13/05 2017

v1.0.4

1.0.4.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

11/05 2016

v1.0.3

1.0.3.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

11/05 2016

dev-cli

dev-cli

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

29/03 2016

v1.0.2

1.0.2.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

29/03 2016

dev-mb_substr

dev-mb_substr

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

15/12 2015

v1.0.1

1.0.1.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph

11/12 2015

v1.0.0

1.0.0.0

Class that assists in building Open Graph meta tags

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

by Chris Konnertz

laravel laravel4 laravel5 opengraph open graph