Posts Learn Components Snippets Categories Tags Tools About
/

How to Automatically Generate Sitemap on Laravel?

Learn how to easily generate a simple sitemap XML on Laravel to better enhance your Google SEO

2 years ago

6 mins read

2009 views

In this post, you'll be learning to set up Sitemap XML on Laravel application with the help of the Laravel Sitemap Package. The steps are very simple and straightforward so just follow along and let's get this going.

Install Dependencies


Install the dependencies using composer. By default, it will install the latest version of the package, but if you are using an old version of Laravel, do specify the suitable version for your project.
composer require laravelium/sitemap
If you wish to update the default configuration such as the time to cache the sitemap and other settings, do publish the providers by running the command below.
php artisan vendor:publish --provider="Laravelium\Sitemap\SitemapServiceProvider"

Implementation


To generate the sitemap define a new route and call it "sitemap" so the URL that's publicly accessible is "yourapp.test/sitemap". Inside the body of this route, retrieve the sitemap instance and this instance has several methods such as add, render and store.
$sitemap = app('sitemap');
The add method contains the URL, date, priority as well as frequency (param are in order left to right). If you have images for the particular page, do include them in a nested array and they will appear on the sitemap file.
/** add item to the sitemap (url, date, priority, freq) */
$sitemap->add(url('/'), now(), '1.0', 'weekly');
$sitemap->add(url('posts'), now(), '0.8', 'monthly');
If you have multiple pages that's are similar but different content such as a blog post or an article, you can loop them like the example below.
Post::query()
        ->get()
        ->each(fn($post) => $sitemap->add(
            route('posts.show', ['slug' => $post->slug]),
            $post->updated_at,
            '0.7',
            'monthly',
            [ /* If your post has image (e.g. cover) that you want to be indexed */
                [
                    'url' => $post->cover_url,
                    'title' => $post->title,
                    'caption' => $post->caption,
                ]
            ]
        ));
Lastly, return the rendered sitemap data so that it's viewable from the browser.
return $sitemap->render('xml');

Final Code


By now your code will look like this and do visit "/sitemap" path in your application to view the rendered sitemap.
Route::get('sitemap', function () {
    $sitemap = app('sitemap');

    /** add item to the sitemap (url, date, priority, freq) */
    $sitemap->add(url('/'), now(), '1.0', 'weekly');
    $sitemap->add(url('posts'), now(), '0.8', 'monthly');

    Post::query()
        ->get()
        ->each(fn($post) => $sitemap->add(
            route('posts.show', ['slug' => $post->slug]),
            $post->updated_at,
            '0.7',
            'monthly',
            [ /* If your post has image (e.g. cover) that you want to be indexed */
                [
                    'url' => $post->cover_url,
                    'title' => $post->title,
                    'caption' => $post->caption,
                ]
            ]
        ));

    return $sitemap->render('xml');
});
If you would like to save the sitemap in an XML file you can make use of the "store" method from the sitemap class.
$sitemap->store('xml');

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)