Laravel Seo Manager
View Package
Laravel SEO Manager is a comprehensive and fully dynamic package designed to optimize your Laravel application's SEO efforts. It provides an easy-to-use interface for managing and updating SEO tags, including Open Graph (OG), Twitter, and Schema tags, for both static and dynamic URLs. This package ensures your site's SEO is always up-to-date, enhancing visibility and search engine ranking with minimal effort. Whether you're working with a few static pages or a complex dynamic site, Laravel SEO Manager makes SEO management simple and effective.
Installation:
Install the package via Composer:
composer require dipesh79/laravel-seo-manager
Usage:
Publish vendor file for migration file and config file
php artisan vendor:publish
And publish Dipesh79\LaravelSeoManager\LaravelSeoManagerServiceProvider
Config File:
Config file contains default values. You can change them after publishing this file.
<?php return [ /** * Redirect URL from SEO Manager Dashboard. */ 'redirect_url' => '/home', /** * Default Pagination Limit for SEO Manager. */ 'pagination_limit' => 10, /** * Default title for the application. * * The title will be used as the title of the page. */ 'title' => 'Title', /** * Default meta-description for the application. * * The description will be used as the meta description of the page. */ 'description' => 'Description', /** * Default meta-keywords for the application. * * The keywords will be used as the meta-keywords of the page. */ 'keywords' => 'Keywords', /** * Default meta-robots for the application. * * Available options: index, noindex, follow, nofollow. */ 'robots' => 'index, follow', 'og' => [ /** * Default meta-og-title for the application. * * The title will be used as the meta-og-title of the page. */ 'title' => 'Title', /** * Default meta-og-description for the application. * * The description will be used as the meta-og-description of the page. */ 'description' => 'Description', /** * Default meta-og-image for the application. * * The image will be used as the meta-og-image of the page. */ 'image' => 'https://via.placeholder.com/1200x630', /** * Default meta-og-url for the application. * * The URL will be used as the meta-og-url of the page. */ 'url' => 'https://khanaldipesh.com.np/package/laravel-seo-manager', ], 'twitter' => [ /** * Default meta-twitter-card for the application. * * Available options: summary, summary_large_image, app, player. */ 'card' => 'summary_large_image', /** * Default meta-twitter-title for the application. * * The title will be used as the meta-twitter-title of the page. */ 'title' => 'Title', /** * Default meta-twitter-description for the application. * * The description will be used as the meta-twitter-description of the page. */ 'description' => 'Description', /** * Default meta-twitter-image for the application. * * The image will be used as the meta-twitter-image of the page. */ 'image' => 'https://via.placeholder.com/1200x630', ], 'schema' => [ /** * Default meta-schema-type for the application. * * The type will be used as the meta-schema-type of the page. * Available options: WebPage, Article, BlogPosting, NewsArticle, Report, ScholarlyArticle, SocialMediaPosting, TechArticle. */ 'type' => 'WebPage', /** * Default meta-schema-name for the application. * * The name will be used as the meta-schema-name of the page. */ 'name' => 'Name', /** * Default meta-schema-description for the application. * * The description will be used as the meta-schema-description of the page. */ 'description' => 'Description', /** * Default meta-schema-url for the application. * * The url will be used as the meta-schema-url of the page. */ 'url' => 'https://khanaldipesh.com.np/package/laravel-seo-manager', ] ];
Run Migration
php artisan migrate
Preparing SEO Tags to Render
Use the following blade component on your parent layout blade file.
<x-seo />
This component will render following code with respective values.
NOTE: This code also includes title tag to don't forget to remove title tag from your parent layout.
<!-- SEO Tags Generated by Laravel SEO Manager Package. Author: Dipesh79 Version: 1.0.0 url: https://github.com/dipesh79/laravel-seo-manager --> <title>{{ $title }}</title> <meta name="description" content="{{ $description }}"> <meta name="keywords" content="{{ $keywords }}"> <meta name="robots" content="{{ $robots }}"> <link rel="canonical" href="{{ $canonical }}"> <!-- Open Graph Tags --> <meta property="og:title" content="{{ $ogTitle }}"> <meta property="og:description" content="{{ $ogDescription }}"> <meta property="og:image" content="{{ $ogImage }}"> <meta property="og:url" content="{{ $ogUrl }}"> <!-- Twitter Card Tags --> <meta name="twitter:card" content="{{ $twitterCard }}"> <meta name="twitter:title" content="{{ $twitterTitle }}"> <meta name="twitter:description" content="{{ $twitterDescription }}"> <meta name="twitter:image" content="{{ $twitterImage }}"> <!-- Schema Markup --> <script type="application/ld+json"> { "@context": "https://schema.org", "@type": "{{ $schemaType }}", "name": "{{ $schemaName }}", "description": "{{ $schemaDescription }}", "url": "{{ $schemaUrl }}" } </script>
Example of parent layout for frontend
<!-- app.blade.php --> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <x-seo /> </head> <body> @yield('content') </body> </html>
Preparing Model To Access SEO Manager Dashboard
<?php use Dipesh79\LaravelSeoManager\Models\Contracts\SeoUser; class User extends Authenticatable implements SeoUser { use HasFactory, Notifiable; // Check Admin Condition and set to true or false to access respective actions public function canAccessSeoManager(): bool { return true; } public function canCreateSeo(): bool { return true; } public function canUpdateSeo(): bool { return true; } public function canDeleteSeo(): bool { return true; } public function canViewSeo(): bool { return true; } }
Now your user can access the SEO Manager Dashboard on this url: {base_url}/admin/seo.
NOTE: Make sure admin/seo url is not used for other pagesYou can generate SEO setting for guest page in two ways.
1. Using Command
You can use following command.
php artisan seo:generate
2. Using SEO Manager Dashboard
You can use a link from SEO manager dashboard.
NOTE: This package generate SEO setting for guest page only. (i.e. which are available for public only) and cannot generate SEO setting for dynamic url (e.g. Blog View Page for public). You have to create an SEO setting dynamically from your backend when creating model (e.g. /blog/{slug}, for blog, you have to create SEO setting while creating blog from admin panel.
Generate SEO setting for dynamic urls
In order to generate SEO setting for dynamic urls, you have to prepare the model first.
<?php namespace App\Models; use Dipesh79\LaravelSeoManager\Traits\HasSeo; class Blog { use HasSeo; ....... }
While creating blog, you can do following to create SEO setting for the blog.
<?php $blog = Blog::create( [ 'title' => $request->title, 'slug' => $request->slug ] ); $blog->createOrUpdateSeo( [ 'title' => $request->title, // The title tag defines the title of the web page that appears in search engine results // and the browser tab. It is one of the most important on-page SEO elements. 'description' => $request->description, // The meta description tag provides a brief summary of the web page's content. // Search engines often display this summary in search results, making it a key element // for click-through rates. 'keywords' => $request->keywords, // Although less significant for SEO today, the meta keywords tag allows you to specify // a list of keywords relevant to the page's content. 'uri' => 'blog/' . $blog->slug, // This is required // The URI of the page (e.g., 'https://khanaldipesh.com.np/blog/slug'). Here, 'blog/slug' // represents the unique path used to access the page. 'robots' => 'index, follow', // The robots tag contains options that control how search engines index your content // and follow the links on your pages. 'og_title' => $request->title, // The og:title tag is an Open Graph tag that specifies the title of the web page when // it is shared on social media platforms like Facebook. 'og_description' => $request->description, // The og:description tag is an Open Graph tag that provides a brief summary of the page // when shared on social media, similar to the meta description but optimized for social sharing. 'og_url' => url('blog/' . $blog->slug), // The og:url tag is an Open Graph tag that defines the canonical URL of the page, ensuring // that social media platforms link to the correct address. 'twitter_card' => 'summary_large_image', // The twitter:card tag specifies the type of content to be displayed when the page is shared // on Twitter. 'summary_large_image' indicates that a larger image preview will be used. 'twitter_title' => $request->title, // The twitter:title tag defines the title of the web page when it is shared on Twitter. 'twitter_description' => $request->description, // The twitter:description tag provides a brief summary of the page for Twitter shares, // optimized for visibility and engagement on the platform. 'schema_type' => $request->schemaType, // The @type defines the type of content (e.g., Article, BlogPosting, Product) // that this structured data describes. 'schema_name' => $request->schemaName, // The name field provides the name or title of the content being described. 'schema_description' => $request->schemaDescription, // The description field offers a brief description of the content, helping search engines // understand what the page is about. 'schema_url' => url('blog/' . $blog->slug) // The url field specifies the canonical URL of the page or content, ensuring that search engines // link to the correct web address. ]);
To add image, use the following code and make sure to add same mediaCollection name.
if ($request->og_image) { $blog->seo->addMedia($request->og_image)->toMediaCollection('og'); } if ($request->twitter_image) { $blog->seo->addMedia($request->twitter_image)->toMediaCollection('twitter'); }
While creating SEO setting, uri is required and others are optional.
Note: You can use same function for updating as it updates for the related model data.
License: MIT
Author: @Dipesh79
Support: For support, email dipeshkhanal79[at]gmail[dot]com.