Generate Dynamic Sitemap in Laravel

Websolutionstuff | Jul-05-2021 | Categories : Laravel

Here we will see how to generate dynamic sitemap in laravel. As we know sitemap is very important part of seo, sitemap is list of pages of a website within a single domain.

Here we will create dynamic XML sitemap in laravel.sitemap is a blueprint of your website that can help to search engines find, crawl and index all of your website's content So let's see how to create dynamic xml sitemap in laravel and how to add sitemap in laravel, 

Here, we are using sitemap package for generate dynamic sitemap in laravel.run below command in your terminal to install package in laravel.

composer require watson/sitemap

 

To publish the configuration file for the sitemap package, run below Artisan command.

php artisan config:publish watson/sitemap

php artisan vendor:publish --provider="Watson\Sitemap\SitemapServiceProvider"

 

Here, you need to add tags for each item in your sitemap using Sitemap::addTag($location, $lastModified, $changeFrequency, $priority). And we can return the sitemap with Sitemap::renderSitemap(). $lastModified variable will be parsed and convered to the right format for the sitemap.

If you want to get raw XML then simply call Sitemap::xml()

Example :

namespace App\Http\Controllers;

use Post;
use Sitemap;

class SitemapsController extends Controller
{
    public function posts()
    {
        $posts = Post::all();

        foreach ($posts as $post) {
            Sitemap::addTag(route('posts.show', $post), $post->updated_at, 'daily', '0.6');
        }

        return Sitemap::render();
    }
}

 

Image Sitemap :

Parameters :  $tag->addImage($location, $caption, $geoLocation, $title, $licenceUrl);

 

namespace App\Http\Controllers;

use Page;
use Sitemap;

class SitemapsController extends Controller
{
    public function pages()
    {
        $pages = Page::all();

        foreach ($pages as $page) {
            $tag = Sitemap::addTag(route('pages.show', $page), $page->updated_at, 'daily', '0.8');

            foreach ($page->images as $image) {
                $tag->addImage($image->url, $image->caption);
            }
        }

        return Sitemap::render();
    }
}

 

Recommended Post
Featured Post
Laravel 9 Livewire Sweetalert Example
Laravel 9 Livewire Sweetalert...

In this article, we will see the laravel 9 livewire sweet alert example. Here we will learn how to use sweetalert i...

Read More

Nov-25-2022

How To Generate Barcode In Laravel
How To Generate Barcode In Lar...

In this tutorial, I will show you how to generate barcodes using the milon/barcode package. In this example, we wil...

Read More

Jun-06-2020

Laravel 8 Export Buttons In Datatables Example
Laravel 8 Export Buttons In Da...

In this article, we will see an example of laravel 8 export buttons in datatables. If you want to export data...

Read More

Oct-14-2020

How to Get All Routes in Laravel 10
How to Get All Routes in Larav...

Hey there! This tutorial guides you through the process of retrieving a comprehensive list of all routes in a Larav...

Read More

Dec-13-2023