ezitisitis/laravel-open-graph

Setting Open Graph for Laravel.

1.2.1 2023-08-24 21:50 UTC

This package is auto-updated.

Last update: 2024-11-08 19:08:18 UTC


README

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

Setting Open Graph for Laravel.

Install

Via Composer install:

composer require ezitisitis/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 Ezitisitis\LaravelOpenGraph\Facades\OpenGraph;

class HomeController extends Controller
{
    public function index()
    {
        OpenGraph::start()
            ->setTitle()
            ->setDescription('The site description...')
            ->setImage(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 Ezitisitis\LaravelOpenGraph\Facades\OpenGraph;

class ArticleController extends Controller
{
    public function show(Article $article)
    {
        OpenGraph::start()
            ->setType('article')
            ->setTitle($article->title)
            ->setDescription($article->description)
            ->setImage($article->thumbnail)
            ->setData([
                'article:published_time' => $article->created_at->toIso8601String(),
            ]);

        return view('home');
    }
}

Credits