📜  laravel seo - PHP (1)

📅  最后修改于: 2023-12-03 14:43:45.889000             🧑  作者: Mango

Laravel SEO - PHP

Laravel SEO is a PHP package that helps in optimizing your Laravel application for search engine ranking. This package adds essential SEO functionalities to your Laravel project, making it easier to optimize your web pages for search engines.

Features
  • Automatic generation of meta tags including, title, description and keywords
  • Open Graph functionality
  • Twitter Card functionality
  • Canonicalization of URL’s
  • Google Analytics integration
Installation

You can install Laravel SEO using Composer. Run the following command in your terminal:

composer require glacieren/laravel-seo
Usage
Set up

Before using any of the functionalities of Laravel SEO, you need to set up. Create a new SEOConfig by running the following command:

php artisan vendor:publish --provider="Glacieren\SEO\SEOServiceProvider" --tag=config

This command will generate a new seo.php file in your config directory. This file contains a configuration array where you can set the default values of your meta tags.

Generating Meta Tags

To generate meta tags, use the SEOMeta facade. Add the following code to the head section of your HTML layout file:

<head>
    {!! SEOMeta::generate() !!}
</head>

This will generate meta tags for the current page with default values that you set up in the seo.php file. You can also set the values of meta tags dynamically in your controller.

public function index()
{
    SEOMeta::setTitle('Homepage');
    SEOMeta::setDescription('This is the description of the homepage.');
    
    return view('welcome');
}
Open Graph and Twitter Card Tags

To generate Open Graph and Twitter Card tags, use the SEOOpenGraph and SEOTwitter facades. Add the following code to the head section of your HTML layout file:

<head>
    {!! SEOOpenGraph::generate() !!}
    {!! SEOTwitter::generate() !!}
</head>

This will generate Open Graph and Twitter Card tags for the current page with default values that you set up in the seo.php file. You can also set the values of Open Graph and Twitter Card tags dynamically in your controller.

public function index()
{
    SEOOpenGraph::setTitle('Homepage');
    SEOOpenGraph::setDescription('This is the description of the homepage.');
    SEOOpenGraph::setType('website');
    SEOOpenGraph::setUrl('https://example.com');
    SEOOpenGraph::addImage('https://example.com/images/logo.png');
    
    SEOTwitter::setTitle('Homepage');
    SEOTwitter::setDescription('This is the description of the homepage.');
    SEOTwitter::setType('summary');
    SEOTwitter::setUrl('https://example.com');
    
    return view('welcome');
}
Canonicalization

To add canonical URL meta tags to your web pages, use the SEOCannonical facade. Add the following code to the head section of your HTML layout file:

<head>
    {!! SEOCannonical::generate() !!}
</head>

This will generate canonical URL meta tags for the current page with the default URL value that you set up in the seo.php file. You can also set the canonical URL value dynamically in your controller.

public function about()
{
    SEOCannonical::setUrl('https://example.com/about');
    
    return view('about');
}
Google Analytics Integration

Laravel SEO integrates Google Analytics in your Laravel project. To use this functionality, you need to set up Google Analytics in your Laravel project. Add your Google Analytics tracking code to your HTML layout file.

<head>
    <meta name="google-analytics" content="{{ env('GOOGLE_ANALYTICS') }}">
</head>

You can also set up Google Analytics in the seo.php file.

return [
    'google_analytics' => env('GOOGLE_ANALYTICS'),
];
Conclusion

Laravel SEO is a powerful package for optimizing your Laravel application for search engine ranking. With its powerful features, Laravel SEO makes it easier for you to optimize your web pages for search engines.