Sitemap Laravel - Comprehensive Guide

Sitemap Laravel - Comprehensive Guide

1. Introduction to Sitemap in Laravel Blade

Sitemap in Laravel Blade (Sitemap Laravel) is an essential tool that helps optimize SEO and provides information about the website structure for search engines. If your website has many subpages or products, a sitemap helps Google bots easily find and understand the relationships between them.

P2PTuts.com will introduce two methods to create a sitemap in Laravel Blade. The integration of a sitemap can be conveniently done with the support of the spatie/laravel-sitemap extension or by coding directly without using any plugins.

Now, let's start implementing Laravel Sitemap.

2. Structure of a Laravel Sitemap

In a sitemap, there are two common formats:

  • A regular sitemap includes parts such as URL, last modification date, priority, and update frequency. This helps search engines evaluate the importance of each page.
<url>
    <loc>{URL}</loc>
    <lastmod>{Last Modification Date}</lastmod>
    <priority>{Priority}</priority>
    <image:image>
        <image:loc>{Image Path}</image:loc>
    </image:image>
    <changefreq>{Update Frequency}</changefreq>
</url>
  • If you have many URLs, you can split them into multiple sitemaps. The following code represents the root of a sitemap:
<sitemap>
    <loc>URL Sitemap</loc>
</sitemap>

3. Creating Sitemap in Laravel Blade using the Package

Use the spatie/laravel-sitemap extension to create a sitemap. Register a route and use the provided function to generate the sitemap and save it to public/sitemap.xml.

use Spatie\Sitemap\SitemapGenerator;

Route::get('/generate-sitemap', function () {
    SitemapGenerator::create(config('app.url'))
        ->writeToFile(public_path('sitemap.xml'));

    return 'Sitemap generated successfully!';
});

Use the function to add URLs to the sitemap and customize properties such as the last modification date, priority, and update frequency.

use Spatie\Sitemap\SitemapGenerator;

Route::get('/generate-sitemap', function () {
    SitemapGenerator::create(config('app.url'))
        ->add('/page1')
        ->add('/page2', now(), '0.9', 'weekly')
        ->writeToFile(public_path('sitemap.xml'));

    return 'Sitemap generated successfully!';
});

There are many types of sitemaps, such as the main sitemap, post sitemap, or product sitemap. Each type reflects an important part of the website.

// Sitemap for the main page
SitemapGenerator::create(config('app.url'))
    ->add('/home')
    ->writeToFile(public_path('sitemap-home.xml'));

// Sitemap for posts
SitemapGenerator::create(config('app.url'))
    ->add('/posts/1')
    ->add('/posts/2')
    ->writeToFile(public_path('sitemap-posts.xml'));

4. Manually Creating a Sitemap with Laravel Blade Sitemap

First, create a route in web.php to handle the process:

Route::get('sitemap.xml', [\App\Http\Controllers\SitemapController::class, 'index']);

Next, in SitemapController.php, load the necessary data to return to the Blade:

public function index(Response $response)
{
    $posts = Post::query()->select('title', 'slug', 'title', 'thumb', 'updated_at')
            ->where('is_active', 1)
            ->get();

    $menus = Menu::query()->get();

    return $response->setContent(view('sitemap/sitemap', compact('posts', 'menus')))
        ->header('content-type', 'text/xml');
}

Finally, in views, render the data to the sitemap/sitemap.blade.php file:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- URL for the home page -->
    <url>
        <loc>{{ url('/') }}</loc>
        <changefreq>daily</changefreq>
        <priority>1</priority>
        <image:image>
            <image:loc>{{ url('template/images/logo.png') }}</image:loc>
        </image:image>
    </url>

    <!-- URLs for menu items -->
    @foreach($menus as $menu)
        <url>
            <loc>{{ route('menu', ['slug' => $menu->slug]) }}</loc>
            <changefreq>daily</changefreq>
            <priority>0.9</priority>
            <image:image>
                <image:loc>{{ asset($menu->thumb) }}</image:loc>
            </image:image>
        </url>
    @endforeach

    <!-- URLs for posts -->
    @foreach($posts as $item)
        <url>
            <loc>{{ route('post', ['slug' => $item->slug])  }}</loc>
            <changefreq>daily</changefreq>
            <priority>0.8</priority>
            <image:image>
                <image:loc>{{ asset($item->thumb) }}</image:loc>
            </image:image>
            <lastmod>{{ date('c', strtotime($item->updated_at)) }}</lastmod>
        </url>
    @endforeach
</urlset>

After completion, you can access https://p2ptuts.com/sitemap.xml to view the result. Don't forget to change the domain from https://p2ptuts.com to yours.

5. Conclusion

P2PTuts.com has guided you through creating a sitemap in Laravel using two methods. With this approach, you can leverage the power of Blade and Blade XML to easily and effectively generate a sitemap in Laravel.

Keywords:

  • Composer require spatie laravel sitemap
  • ultrono laravel sitemap
  • Spatie laravel-sitemap
  • laravel sitemap dynamic generate
  • Sitemap laravel
  • Create sitemap laravel 8
  • XML sitemap
  • Laravel-Sluggable