Laravel SEO is a package designed to help manage SEO meta tags and improve the search engine optimization (SEO) of your Laravel applications. It provides an easy way to handle meta tags, social media sharing cards, and other important elements that enhance the visibility and attractiveness of your site in search engines and on social media platforms.
Key Features
- Easy Meta Tag Management: Simplifies the process of adding and managing SEO-related meta tags in your views.
- Social Media Integration: Support for Open Graph and Twitter Card tags to optimize sharing on social media.
- SEO-Friendly URLs: Automatically generate SEO-friendly URLs for your application.
- Customizable Tags: Allow customization of meta tags for different pages, including dynamic content.
Installation
You can install the package via Composer:
1 |
composer require artesaos/seotools |
Configuration
After installing the package, you can publish the configuration file if needed:
1 |
php artisan vendor:publish --provider="Artesaos\SEOTools\Providers\SEOToolsServiceProvider" |
This command will create a configuration file at config/seotools.php
, where you can adjust various settings.
Basic Usage
Setting Meta Tags
You can set meta tags in your controllers or middleware using the provided SEOTools facade:
1 2 3 4 5 6 7 8 9 10 |
use SEOTools; public function index() { SEOTools::setTitle('Your Page Title'); SEOTools::setDescription('Your page description goes here.'); SEOTools::setCanonical('https://yourdomain.com/page'); return view('your.view'); } |
Setting Open Graph Tags
For social media optimization, you can also set Open Graph tags:
1 2 3 4 |
SEOTools::opengraph()->setTitle('Your Open Graph Title'); SEOTools::opengraph()->setDescription('Your Open Graph Description'); SEOTools::opengraph()->setUrl('https://yourdomain.com/page'); SEOTools::opengraph()->addImage('https://yourdomain.com/image.jpg'); |
Setting Twitter Card Tags
Similarly, you can set Twitter card tags:
1 2 3 4 |
SEOTools::twitter()->setTitle('Your Twitter Card Title'); SEOTools::twitter()->setDescription('Your Twitter Card Description'); SEOTools::twitter()->setUrl('https://yourdomain.com/page'); SEOTools::twitter()->addImage('https://yourdomain.com/image.jpg'); |
Blade Integration
To include the generated meta tags in your Blade views, you can use the following directives in your layout file (usually in layouts/app.blade.php
):
1 |
{!! SEOTools::generate() !!} |
This will render all the meta tags, Open Graph tags, and Twitter Card tags you’ve set in your controllers or middleware.
Dynamic Meta Tags
If you have dynamic content, you can set meta tags based on the content of your pages. For example, in a blog post controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function show(Post $post) { SEOTools::setTitle($post->title); SEOTools::setDescription($post->excerpt); SEOTools::setCanonical(route('posts.show', $post)); // Open Graph SEOTools::opengraph()->setTitle($post->title); SEOTools::opengraph()->setDescription($post->excerpt); SEOTools::opengraph()->setUrl(route('posts.show', $post)); SEOTools::opengraph()->addImage($post->image); return view('posts.show', compact('post')); } |
Conclusion
Laravel SEO provides a robust way to manage SEO meta tags in your Laravel applications, helping you improve your website’s visibility and sharing capabilities on social media. By using this package, you can easily manage meta tags and ensure that your application is optimized for search engines.