Home / Tutorials / How to Automatically Generate Sitemap on Laravel?
How to Automatically Generate Sitemap on Laravel? cover

How to Automatically Generate Sitemap on Laravel?

6 mins

1.8K

3 years ago

0 comments

Skilled

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');
notion avatar

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Support Us

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

Related Tutorials

Code Snippets

Collection of Laravel, AlpineJS, VueJS, Tailwind CSS, Flutter snippets and more. New snippets added every day.
View Snippets

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this