fabpl/laravel-seo

A Laravel-native package to manage SEO-related HTML tags with precision and developer experience in mind.

1.0.0 2025-07-11 10:32 UTC

This package is auto-updated.

Last update: 2025-07-11 10:35:59 UTC


README

A Laravel-native package to manage SEO-related HTML tags with precision and developer experience in mind.
Supports title, meta, og:*, twitter:*, and canonical tags โ€” nothing more, nothing less.

โœจ Features

  • Fluent, expressive API via a Seo facade
  • Supports standard SEO, Open Graph, and Twitter tags
  • Automatically renders tags via the @seo Blade directive
  • Enum-based tag validation for strict SEO compliance
  • Default fallback values via config/seo.php
  • Auto-injects <link rel="canonical"> (optional)

๐Ÿš€ Installation

composer require fabpl/laravel-seo

Publish the config file:

php artisan vendor:publish --tag=seo-config

โš™๏ธ Configuration

Edit the config/seo.php file to set global defaults:

return [

    /*
    |--------------------------------------------------------------------------
    | Default Title
    |--------------------------------------------------------------------------
    |
    | This title will be used if no title is explicitly set via Seo::title().
    |
    */
    'title' => env('APP_NAME', 'Laravel'),

    /*
    |--------------------------------------------------------------------------
    | Default Meta Tags
    |--------------------------------------------------------------------------
    |
    | Meta tags that should be included by default on all pages.
    | Only tags defined in MetaTags enum are supported.
    |
    */
    'meta' => [
        'author' => '',
        'description' => '',
        'keywords' => '',
        'robots' => 'index, follow',
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Open Graph Tags
    |--------------------------------------------------------------------------
    |
    | Open Graph tags included on all pages. Keys must match values from
    | the OpenGraphTags enum.
    |
    */
    'open_graph' => [
        'og:description' => '',
        'og:image' => '',
        'og:locale' => env('APP_LOCALE', 'en'),
        'og:site_name' => env('APP_NAME', 'Laravel'),
        'og:title' => env('APP_NAME', 'Laravel'),
        'og:type' => 'website',
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Twitter Tags
    |--------------------------------------------------------------------------
    |
    | Twitter card tags included by default. Keys must match values from
    | the TwitterTags enum.
    |
    */
    'twitter' => [
        'twitter:card' => 'summary_large_image',
        'twitter:creator' => '', // e.g. '@username'
        'twitter:description' => '',
        'twitter:image' => '',
        'twitter:site' => '', // e.g. '@yourhandle'
        'twitter:title' => env('APP_NAME', 'Laravel'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Auto Canonical URL
    |--------------------------------------------------------------------------
    |
    | When enabled, the current URL will be automatically added as a canonical
    | tag unless manually overridden via Seo::canonical().
    |
    */
    'auto_canonical' => true,

];

โœ… Usage

In a controller:

use Fabpl\Seo\Facades\Seo;
use Fabpl\Seo\Enums\MetaTags;
use Fabpl\Seo\Enums\OpenGraphTags;
use Fabpl\Seo\Enums\TwitterTags;

Seo::title('Laravel Seo Package')
    ->description('A Laravel-native package to manage SEO-related HTML tags with precision and developer experience in mind.')
    ->twitter(TwitterTags::Creator, '@fabpl')
    ->canonical('https://github.com/fabpl/laravel-seo');

In a Blade layout:

<head>
    ...
    @seo
</head>

๐Ÿ” Strict SEO Tag Validation

Only officially supported SEO tags are allowed. All supported keys are defined as PHP enum classes:

  • Fabpl\Seo\Enums\MetaTags
  • Fabpl\Seo\Enums\OpenGraphTags
  • Fabpl\Seo\Enums\TwitterTags

Invalid keys throw exception.

๐Ÿ“ฆ API Reference

Seo::title(string $title): self;
Seo::description(string $description): self;
Seo::keywords(array $keywords): self;
Seo::author(string $author): self;
Seo::canonical(string $url): self;
Seo::meta(MetaTags $name, string $content): self;
Seo::openGraph(OpenGraphTags $name, string $content): self;
Seo::twitter(TwitterTags $name, string $content): self;
Seo::render(): HtmlString;

๐Ÿงช Testing

Tests are included and can be run with:

composer test

๐Ÿ“„ License

MIT ยฉ Fabrice