How to Create XML Sitemap in Laravel

Do you want to create an XML sitemap in Laravel? The XML sitemap is a special document that lists all pages of your website. Google looks into your XML sitemap and crawls them all. The search engine gets an idea about your website structure through this document. As a website owner, you must create the XML sitemap which helps to rank better in search engine results.

Create XML Sitemap in Laravel

In Laravel, one can generate sitemaps by writing a custom code. In that case, you may need to fetch all your pages from the database and add the page links to the sitemap file.

The other option is to add all your routes manually (instead of fetching them from the database) and generate a sitemap out of it.

We are going to see a third option where you neither need to fetch pages from the database nor follow a manual process. Instead, it will crawl your website and generate a sitemap automatically.

For this, you need to install the package spatie/laravel-sitemap using the command below.

composer require spatie/laravel-sitemap

Create a route to generate XML sitemap as follows.

Route::get('/generate-sitemap', 'SitemapController@generatesitemap');

Next, create a controller SitemapController using artisan command.

php artisan make:controller SitemapController

Add the code below in the SitemapController.php file.

use Spatie\Sitemap\SitemapGenerator;

class SitemapController extends Controller
{
    public function generatesitemap()
    {
        $path = public_path('sitemap.xml');
        SitemapGenerator::create(env('APP_URL'))->writeToFile($path);
    }
}

I have passed a ‘public’ directory path as our sitemap.xml document should be inside this folder. The package takes a site URL from the environment variable ‘APP_URL’.

Run the SITE_URL/generate-sitemap on the browser. Access your sitemap at SITE_URL/sitemap.xml, you should now see your XML sitemap with all your pages listed.

Add Routes Manually

The package also allows you to add URLs manually in your sitemap. You can do it using the code as shown below.

use Spatie\Sitemap\SitemapGenerator;
use Carbon\Carbon;
use Spatie\Sitemap\Tags\Url;

class SitemapController extends Controller
{
    public function generatesitemap()
    {
        $path = public_path('sitemap.xml');
        SitemapGenerator::create(env('APP_URL'))
            ->getSitemap()
            ->add(Url::create('/payment')
                    ->setLastModificationDate(Carbon::yesterday())
                    ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
                    ->setPriority(0.1))
            ->add(Url::create('/services')
                    ->setLastModificationDate(Carbon::yesterday())
                    ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
                    ->setPriority(0.1))
                ->writeToFile($path);
    }
}

This is how we can easily create XML sitemap in Laravel. You may read more about this package on the documentation.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *