How To Generate A Sitemap In Laravel 6/7/8/9

  • 19-09-2022
  • 1184
  • Laravel 9
  • Haresh Chauhan

How To Generate A Sitemap In Laravel; Using composer require laravelium/sitemap we will generate a sitemap for our site in this tutorial example, Sitemap is too much important while you are working on a public website and want to invite the public from search engine organic browser. While you need to be required to submit your site to any search engine you must need to submit the theme and a site map. Based on that site map the search engine will Crowle your site.

using Laravel sitemap generator composer we will generate a site map with an example, in this example, we will create a dynamic sitemap and also a static sitemap.

We will create an XML sitemap in this tutorial, I have also given a different version package composer you can choose from and install in your application. having ended this tutorial you will easy to generate a sitemap for your web application based on route names.

Preview:

Install Composer

Use the below-suggested command in your terminal and install sitemap to generate composer package dependency in your laravel application. copy and paste this command into your project terminal.

composer require laravelium/sitemap

Version (Optional)

If you want to look at a specific version package installation in your application find the form here. and add in your composer JSON file.

For Laravel 8
"laravelium/sitemap": "8.*"
For Laravel 8 (development branch)
"laravelium/sitemap": "8.x-dev"
For Laravel 7
"laravelium/sitemap": "7.*"
For Laravel 7 (development branch)
"laravelium/sitemap": "7.x-dev"
For Laravel 6
"laravelium/sitemap": "6.*"
For Laravel 6 (development branch)
"laravelium/sitemap": "6.x-dev"
For Laravel 5.8
"laravelium/sitemap": "3.1.*"
For Laravel 5.8 (development branch)
"laravelium/sitemap": "3.1.x-dev"
For Laravel 5.7
"laravelium/sitemap": "3.0.*"
For Laravel 5.7 (development branch)
"laravelium/sitemap": "3.0.x-dev"
For Laravel 5.6
"laravelium/sitemap": "2.8.*"
For Laravel 5.6 (development branch)
"laravelium/sitemap": "2.8.x-dev"
For Laravel 5.5
"laravelium/sitemap": "2.7.*"
For Laravel 5.5 (development branch)
"laravelium/sitemap": "2.7.x-dev"

Once you add the selected version to your application composer.json file, we need to install composer using the below provided command. So it will install only your application selected version only.

composer update

Once you install composer we meed to publish an asset config file for the sitemap to make it a stylish and pretty URL.

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

Dynamic Usage

You can use it in a loop with the collected database data, and for that, you need to follow the given example.

Route::get('post/{title}',[HomeController::class,'post'])->name('single.post');
$sitemap = \App::make('sitemap');

$posts = Post::orderBy('id','desc')->where('status',1)->get();

foreach ($posts as $post) {

     $sitemap->add(URL::to(route('single.post',$post->slug)),$post->created_at,1);
}

// GENERATE SITEMAP
$sitemap->store('xml','sitemap',base_path('/'));

This sitemap will generate and store into your application root path, you can change the path according to your requirement.

Static Usage

You can also give your route with the static name, For example, you need to first create all the routes in your web.php file with the name.

Route::get('categories.html',[CategoryFrontController::class,'categoryCount'])->name('categories');
Route::get('about-us.html',[HomeController::class,'termCondition'])->name('about');
Route::get('contact-us.html',[ContactUsFrontController::class,'contactUs'])->name('contact');

Call the router with the given name in the sitemap argument as below given example.

$sitemap = \App::make('sitemap');

$sitemap->add(URL::to(route('categories')),now(),0.8);
$sitemap->add(URL::to(route('about')),now(),0.8);
$sitemap->add(URL::to(route('contact')),now(),0.8);

// GENERATE SITEMAP
$sitemap->store('xml','sitemap',base_path('/'));

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :