PHP code example of magutti / seo-tools

1. Go to this page and download the library: Download magutti/seo-tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

magutti / seo-tools example snippets


return [
    'inertia' => false,  // set to true to enable Inertia.js support
    'meta' => [
        'defaults' => [
            'title'       => false,
            'titleBefore' => false,
            'description' => false,
            'separator'   => ' - ',
            'keywords'    => [],
            'canonical'   => false,
            'robots'      => false,
        ],
        'webmaster_tags' => [
            // 'google'    => 'your-code',
            // 'bing'      => 'your-code',
            // 'alexa'     => 'your-code',
            // 'pinterest' => 'your-code',
            // 'yandex'    => 'your-code',
            // 'norton'    => 'your-code',
        ],
    ],
    'opengraph' => [
        'defaults' => [
            'title'       => false,
            'description' => false,
            'url'         => false,
            'type'        => false,
            'site_name'   => false,
            'images'      => [],
        ],
    ],
    'twitter'  => ['defaults' => []],
    'json-ld'  => [
        'defaults' => [
            'title'       => false,
            'description' => false,
            'url'         => false,
            'type'        => 'WebPage',
            'images'      => [],
        ],
    ],
];

use Magutti\SeoTools\Facades\SEOTools as SEO;

class ArticleController extends Controller
{
    public function show(Article $article)
    {
        SEO::setTitle($article->title)
           ->setDescription($article->excerpt)
           ->setCanonical(route('articles.show', $article))
           ->addImages([$article->cover_url]);

        return view('articles.show', compact('article'));
    }
}

use Magutti\SeoTools\Traits\SEOTools;

class PostController extends Controller
{
    use SEOTools;

    public function show(Post $post)
    {
        $this->seo()->setTitle($post->title);
    }
}

use Magutti\SeoTools\Facades\SEOMeta;

SEOMeta::setTitle('Page Title')
       ->setDescription('Page description')
       ->setKeywords(['laravel', 'seo'])
       ->setCanonical('https://example.com/page')
       ->setRobots('index,follow')
       ->addAlternateLanguage('it', 'https://example.com/it/page')
       ->setPrev('https://example.com/page/1')
       ->setNext('https://example.com/page/3');

use Magutti\SeoTools\Facades\OpenGraph;

OpenGraph::setTitle('Page Title')
         ->setDescription('Page description')
         ->setUrl('https://example.com/page')
         ->setType('article')
         ->setSiteName('My Site')
         ->addImage('https://example.com/image.jpg');

// Article-specific properties
OpenGraph::setArticle([
    'published_time' => '2024-01-01',
    'author'         => 'Marco Asperti',
    'section'        => 'Technology',
    'tags'           => ['laravel', 'seo'],
]);

use Magutti\SeoTools\Facades\TwitterCard;

TwitterCard::setType('summary_large_image')
           ->setSite('@mysitehandle')
           ->setTitle('Page Title')
           ->setDescription('Page description')
           ->setImage('https://example.com/image.jpg');

use Magutti\SeoTools\Facades\JsonLd;

JsonLd::setType('Article')
      ->setTitle('Article Title')
      ->setDescription('Article description')
      ->setUrl('https://example.com/article')
      ->addImage('https://example.com/image.jpg')
      ->addValue('author', ['@type' => 'Person', 'name' => 'Marco Asperti'])
      ->addValue('datePublished', '2024-01-01');

use Magutti\SeoTools\Facades\JsonLdMulti;

// First block (index 0 is created automatically)
JsonLdMulti::setType('Article')->setTitle('Article Title');

// Add a second block
JsonLdMulti::newJsonLd()
           ->setType('BreadcrumbList')
           ->addValue('itemListElement', [/* breadcrumb items */]);

// Switch between blocks
JsonLdMulti::select(0)->setDescription('Updated description');

use Magutti\SeoTools\Contracts\SEOFriendly;
use Magutti\SeoTools\Contracts\MetaTags;
use Magutti\SeoTools\Contracts\OpenGraph;
use Magutti\SeoTools\Contracts\TwitterCards;
use Magutti\SeoTools\Contracts\JsonLd;

class Article extends Model implements SEOFriendly
{
    public function loadSEO(MetaTags $meta, OpenGraph $og, TwitterCards $twitter, JsonLd $jsonLd): void
    {
        $meta->setTitle($this->title)->setDescription($this->excerpt);
        $og->setTitle($this->title)->addImage($this->cover_url);
        $twitter->setTitle($this->title)->setImage($this->cover_url);
        $jsonLd->setType('Article')->setTitle($this->title);
    }
}

use Magutti\SeoTools\Traits\SEOTools;

class ArticleController extends Controller
{
    use SEOTools;

    public function show(Article $article)
    {
        $this->loadSEO($article);
        return view('articles.show', compact('article'));
    }
}
bash
php artisan vendor:publish --provider="Magutti\SeoTools\Providers\SeoToolsServiceProvider"