Laravel 10 - How to Generate Sitemap

Touseef Afridi
09 Sep 24

Laravel 10 - How to Generate Sitemap

In this tutorial, we will discuss how to generate a sitemap in Laravel 10. A sitemap improves SEO by helping search engines index your site efficiently.


If you're a video person, feel free to skip the post and check out the video instead!


Step # 1 : Create fresh Laravel project or use existing project.

Two commands to create fresh laravel project.
Global Command : laravel new sitemap
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist sitemap

Step # 2 : Access the project.

Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/sitemap
Next, install the required dependencies and run the Laravel Vite development server for front-end assets:
Command : npm install && npm run dev
In a new terminal window or tab (while keeping the Vite server running), navigate to the same project directory to execute further Laravel command.

Step # 3 : Install the package.

Command : composer require spatie/laravel-sitemap

Step # 4 : Publish the configuration.

Command : php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config

Step # 5 : Create views.

To test the sitemap, create views named about.blade.php and contact.blade.php
Update about.blade.php as follows
<!DOCTYPE html>
<html>
<head>
 <title>About - Code Shotcut</title>
</head>
<body>
 <h1>About Page</h1>
 <p>Some description related to the about page.</p>
</body>
</html>
Update contact.blade.php as follows
<!DOCTYPE html>
<html>
<head>
 <title>Contact - Code Shotcut</title>
</head>
<body>
 <h1>Contact Page</h1>
 <p>Some description related to the contact page.</p>
</body>
</html>

Step # 6 : Update web.php.

Create routes for the about and contact views, and a sitemap route to generate the sitemap manually.
<?php
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use Carbon\Carbon;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
Route::get('/', function(){
 return view('welcome');
});
Route::get('/about', function(){
 return view('about');
});
Route::get('/contact', function(){
 return view('contact');
});
Route::get('/sitemap', function () {
 //Lets generate sitemap manually
 $sitemap = Sitemap::create()
 ->add(Url::create('/'))
 ->add(Url::create('/about'))
 ->add(Url::create('/contact'));
 $sitemap->writeToFile(public_path('sitemap.xml'));
 return "success";
});

Step # 7 : It's time to test.

Start the Laravel development server.
Command : php artisan serve.
Let's generate sitemap. Access below URL.
127.0.0.1:8000/sitemap
It should return success.


Let's view the sitemap.xml on browser. Access below URL.
127.0.0.1:8000/sitemap.xml


You can add dynamic pages like below.
Route::get('/sitemap', function () {
 $sitemap = Sitemap::create()
 ->add(Url::create('/'))
 ->add(Url::create('/about'))
 ->add(Url::create('/contact'));
 // Similarly you can add dynamic pages
 // For example dynamic pages for : Tags, Posts, Categories etc
 // For Dynamic pages.
 $posts = Post::all();
 foreach ($posts as $post) {
  $sitemap->add("/post/{$post->slug}");
 }
 $sitemap->writeToFile(public_path('sitemap.xml'));
 return "success";
});
Furthermore you can change frequency, priority etc.
Route::get('/sitemap', function () {
 $sitemap = Sitemap::create()
 ->add(Url::create('/')
 ->setLastModificationDate(Carbon::yesterday())
 ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
 ->setPriority(0.1));
 $sitemap->writeToFile(public_path('sitemap.xml'));
});
For the live environment, you can create a cron job to automatically generate or update the sitemap.


Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering