ycs77/laravel-open-graph

Setting Open Graph for Laravel.

v0.1.1 2022-02-09 10:25 UTC

This package is auto-updated.

Last update: 2022-07-09 11:35:20 UTC


README

Latest Version on Packagist Software License CI Build Status Style CI Build Status Total Downloads

Setting Open Graph for Laravel.

Install

Via Composer install:

composer require ycs77/laravel-open-graph

Include Open Graph meta view into your layout <head>:

@include('open-graph::meta')

Usage

Set Open Graph metadata into Controller (this example page title default is Laravel app name):

HomeController

<?php

namespace App\Http\Controllers;

use Ycs77\LaravelOpenGraph\Facades\OpenGraph;

class HomeController extends Controller
{
    public function index()
    {
        OpenGraph::start()
            ->title()
            ->description('The site description...')
            ->image(asset('images/og-image.png'));

        return view('home');
    }
}

Set the article's Open Graph metadata Controller (this example article title like Article name - App name):

ArticleController

<?php

namespace App\Http\Controllers;

use App\Article;
use Ycs77\LaravelOpenGraph\Facades\OpenGraph;

class ArticleController extends Controller
{
    public function show(Article $article)
    {
        OpenGraph::start()
            ->type('article')
            ->title($article->title)
            ->description($article->description)
            ->image($article->thumbnail)
            ->data([
                'article:published_time' => $article->created_at->toIso8601String(),
            ]);

        return view('home');
    }
}

Open Graph references: https://developers.facebook.com/docs/sharing/webmasters/