PHP code example of salehye / laravel-seo

1. Go to this page and download the library: Download salehye/laravel-seo 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/ */

    

salehye / laravel-seo example snippets


return [
    // Site Information
    'site_name' => env('SEO_SITE_NAME', 'My Website'),
    'site_url' => env('SEO_SITE_URL', 'https://example.com'),
    'default_title' => env('SEO_DEFAULT_TITLE', 'Home'),
    'default_description' => env('SEO_DEFAULT_DESCRIPTION', 'Default description'),
    'default_image' => env('SEO_DEFAULT_IMAGE', 'images/default-og-image.jpg'),
    
    // Social Media
    'twitter_handle' => env('SEO_TWITTER_HANDLE', '@website'),
    'facebook_app_id' => env('SEO_FACEBOOK_APP_ID'),
    
    // Sitemap
    'sitemap' => [
        'enabled' => true,
        'frequency' => 'daily',
        'priority' => 0.8,
    ],
    
    // Cache
    'cache_enabled' => true,
    'cache_ttl' => 3600,
];



namespace App\Models;

use Salehye\Seo\Traits\HasSeo;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasSeo;
    
    protected function seoConfig(): array
    {
        return [
            'title' => $this->title,
            'description' => $this->excerpt,
            'keywords' => $this->tags?->pluck('name')->join(', '),
            'image' => $this->featured_image,
            'type' => 'article',
            
            'schemas' => [
                [
                    'type' => 'BlogPosting',
                    'headline' => $this->title,
                    'description' => $this->excerpt,
                    'author' => $this->author->name,
                    'publishedAt' => $this->published_at->toIso8601String(),
                    'image' => $this->featured_image,
                ],
            ],
            
            'breadcrumb' => [
                ['name' => 'Home', 'url' => url('/')],
                ['name' => 'Blog', 'url' => route('blog.index')],
                ['name' => $this->title, 'url' => $this->url],
            ],
        ];
    }
}

public function show(Post $post)
{
    $seo = $post->generateSeo();
    
    return view('posts.show', [
        'post' => $post,
        'seo' => $seo,
    ]);
}

use Salehye\Seo\Facades\Seo;

public function index()
{
    $seo = Seo::title('Blog Posts')
        ->description('Read our latest blog posts')
        ->keywords(['blog', 'articles', 'news'])
        ->canonical(route('blog.index'))
        ->image(asset('images/blog-og.jpg'))
        ->addOrganizationSchema()
        ->addWebsiteSchema()
        ->addBreadcrumbSchema([
            ['name' => 'Home', 'url' => url('/')],
            ['name' => 'Blog', 'url' => route('blog.index')],
        ])
        ->generate();
    
    return view('blog.index', ['seo' => $seo]);
}

// In your controller or route
seo_title('Page Title');
seo_description('Page description');
seo_keywords(['keyword1', 'keyword2']);
seo_image('image.jpg');
seo_canonical(route('page'));

// Add schemas
seo_organization_schema();
seo_website_schema();
seo_breadcrumb_schema([...]);
seo_faq_schema([...]);

// Organization
Seo::addOrganizationSchema([...]);

// Website
Seo::addWebsiteSchema([...]);

// WebPage
Seo::addWebPageSchema([...]);

// LocalBusiness
Seo::addLocalBusinessSchema([...]);

// Service
Seo::addServiceSchema([...]);

// Product
Seo::addProductSchema([...]);

// Article / BlogPosting / NewsArticle
Seo::addArticleSchema([...]);
Seo::addBlogPostingSchema([...]);
Seo::addNewsArticleSchema([...]);

// FAQ
Seo::addFaqSchema([
    ['question' => 'Q1', 'answer' => 'A1'],
    ['question' => 'Q2', 'answer' => 'A2'],
]);

// AggregateRating
Seo::addAggregateRatingSchema(4.5, 100); // rating, count

// Review
Seo::addReviewSchema([...]);

// Breadcrumb
Seo::addBreadcrumbSchema([
    ['name' => 'Home', 'url' => url('/')],
    ['name' => 'Category', 'url' => route('category')],
    ['name' => 'Page', 'url' => url()->current()],
]);

// Event
Seo::addEventSchema([...]);

// City
Seo::addCitySchema([...]);

// Person
Seo::addPersonSchema([...]);

// Video
Seo::addVideoSchema([...]);

// Recipe
Seo::addRecipeSchema([...]);

// JobPosting
Seo::addJobPostingSchema([...]);

// Course
Seo::addCourseSchema([...]);

// Get/Set SEO values
seo_title('Title')
seo_description('Description')
seo_keywords(['key1', 'key2'])
seo_image('image.jpg')
seo_canonical('https://example.com/page')
seo_robots('noindex, nofollow')

// Add schemas
seo_schema([...])
seo_organization_schema()
seo_website_schema()
seo_breadcrumb_schema([...])
seo_faq_schema([...])
seo_article_schema([...])
seo_product_schema([...])
seo_aggregate_rating_schema(4.5, 100)

// Open Graph & Twitter
seo_og('title', 'Title')
seo_twitter('card', 'summary_large_image')

// Render all SEO tags
seo_render()

// Utility
seo_default_image()
seo_site_name()
seo_social_links()
seo_verification_codes()
seo_analytics_ids()
seo_sitemap_url()
seo_robots_url()

use Salehye\Seo\Models\SeoMetadata;

// Create or update SEO for a model
SeoMetadata::createOrUpdateForModel($post, [
    'title' => 'SEO Title',
    'description' => 'SEO Description',
    'meta_keywords' => 'keyword1, keyword2',
    'og_image' => 'image.jpg',
    'canonical_url' => route('post.show', $post),
    'schema_data' => [...],
]);

// Get SEO for a model
$seo = SeoMetadata::forModel($post)->first();
$seoArray = $seo->toSeoArray();

'cache_enabled' => true,
'cache_ttl' => 3600, // 1 hour
'cache_prefix' => 'seo_',

// Generate with cache (1 hour TTL)
$seo = $post->generateSeoWithCache(3600);

// Clear cache
$post->clearSeoCache();

// Set alternate languages
Seo::alternate('en', route('page.en'))
    ->alternate('ar', route('page.ar'))
    ->alternate('fr', route('page.fr'));

// Or in model
protected function seoConfig(): array
{
    return [
        'title' => $this->getTranslation('title', app()->getLocale()),
        // ...
    ];
}

class Product extends Model
{
    use HasSeo;
    
    protected function seoConfig(): array
    {
        return [
            'title' => $this->name,
            'description' => $this->short_description,
            'keywords' => $this->tags?->pluck('name')->join(', '),
            'image' => $this->main_image,
            'type' => 'product',
            
            'schemas' => [
                [
                    'type' => 'Product',
                    'name' => $this->name,
                    'description' => $this->description,
                    'image' => $this->main_image,
                    'price' => $this->price,
                    'currency' => 'SAR',
                    'inStock' => $this->in_stock,
                    'sku' => $this->sku,
                    'brand' => $this->brand->name,
                    'rating' => [
                        'value' => $this->averageRating(),
                        'count' => $this->reviews_count,
                    ],
                ],
            ],
        ];
    }
}

class Service extends Model
{
    use HasSeo;
    
    protected function seoConfig(): array
    {
        return [
            'title' => $this->name,
            'description' => $this->description,
            'image' => $this->image,
            'type' => 'service',
            
            'schemas' => [
                [
                    'type' => 'Service',
                    'name' => $this->name,
                    'description' => $this->description,
                    'areaServed' => ['@type' => 'City', 'name' => $this->city],
                    'offers' => [
                        'price' => $this->starting_price,
                        'currency' => 'SAR',
                    ],
                ],
                $this->faqs ? [
                    'type' => 'FAQ',
                    'faqs' => $this->faqs,
                ] : null,
            ],
        ];
    }
}

class Post extends Model
{
    use HasSeo;
    
    protected function seoConfig(): array
    {
        return [
            'title' => $this->title,
            'description' => $this->excerpt,
            'keywords' => $this->tags?->pluck('name')->join(', '),
            'image' => $this->featured_image,
            'type' => 'article',
            
            'meta' => [
                'author' => $this->author->name,
                'published_date' => $this->published_at->format('Y-m-d'),
                'category' => $this->category->name,
            ],
            
            'schemas' => [
                [
                    'type' => 'BlogPosting',
                    'headline' => $this->title,
                    'description' => $this->excerpt,
                    'author' => $this->author->name,
                    'publishedAt' => $this->published_at->toIso8601String(),
                    'modifiedAt' => $this->updated_at->toIso8601String(),
                    'image' => $this->featured_image,
                ],
            ],
        ];
    }
}
bash
php artisan vendor:publish --provider="Salehye\Seo\Providers\SeoServiceProvider" --tag="seo-config"
bash
php artisan vendor:publish --provider="Salehye\Seo\Providers\SeoServiceProvider" --tag="seo-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Salehye\Seo\Providers\SeoServiceProvider" --tag="seo-views"
bash
php artisan seo:install
bash
php artisan seo:install
bash
php artisan seo:clear-cache
bash
php artisan seo:generate-sitemap --output=public/sitemap.xml