How To Create Dynamic XML Sitemap In Laravel 9

Websolutionstuff | Jun-08-2022 | Categories : Laravel

In this article, we will see how to create a dynamic XML sitemap in laravel 9. As we know sitemap is a very important part of SEO. The sitemap is a list of pages of a website within a single domain. The 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 a dynamic XML sitemap in laravel 9, laravel 9 sitemap dynamic generate, laravel 9 generate a sitemap XML file, how to create XML sitemap in laravel 9, laravel 9 sitemap dynamic generate, sitemap generator in laravel 9.

Step 1: Install Package

In this step, we will install watson/sitemap package using the below command.

composer require watson/sitemap

 

 

Step 2: Configure Package

To publish the configuration file for the sitemap package using the below 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 converted 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();
    }
}

 


You might also like :

Recommended Post
Featured Post
How To Convert Laravel Query To SQL Query
How To Convert Laravel Query T...

In this article, we will see how to convert a laravel query to an SQL query. Many times we required laravel query builde...

Read More

Oct-27-2022

How To Add Toastr Notification In Laravel 10
How To Add Toastr Notification...

In this article, we will see how to add toastr notification in laravel 10. Here, we will learn about toastr notification...

Read More

Mar-06-2023

How To Create Bar Chart In Laravel 9 Using Highcharts
How To Create Bar Chart In Lar...

In this article, we will see how to create a bar chart in laravel 9 using highcharts. A bar chart or bar graph is a...

Read More

Oct-05-2022

Multi Step Form Example In Laravel
Multi Step Form Example In Lar...

Today in this post we will see multi step form example in laravel, here we will create laravel multi step form example.&...

Read More

Jul-21-2021