Creating a sitemap is crucial for search engine optimization (SEO) as it helps search engines discover and index the pages of your website. In Laravel 10, you can generate a sitemap without relying on any external plugins. In this tutorial, we'll guide you through the process of generating a sitemap in Laravel 10 using a product module as an example. Let's dive in!
Please follow below steps to generate custom sitemap in Laravel 10.
Step 1: Set Up Routes and Controller
First, create the necessary routes and a controller to handle the sitemap generation. Open the routes/web.php
file and define a route that points to the controller method. For example:
use App\Http\Controllers\SitemapController;
Route::get('/sitemap.xml', [SitemapController::class, 'generateSitemap']);
Next, create the SitemapController
using the command php artisan make:controller SitemapController
. Open the newly created SitemapController.php
file and add the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\URL;
class SitemapController extends Controller
{
public function generateSitemap()
{
// Initialize XML string
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// Generate sitemap URLs dynamically
$products = \App\Models\Product::all();
foreach ($products as $product) {
$xml .= '<url>';
$xml .= '<loc>' . URL::to('/product/' . $product->id) . '</loc>';
$xml .= '<lastmod>' . $product->updated_at->tz('UTC')->toAtomString() . '</lastmod>';
$xml .= '<changefreq>daily</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
// Return the XML response
return Response::make($xml, 200, [
'Content-Type' => 'application/xml',
]);
}
}
Step 2: Generate Sitemap Route
To generate the sitemap, we need to create a custom Artisan command. Open the app/Console/Commands
directory and create a new file named GenerateSitemap.php
. Add the following code to the file:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\File;
class GenerateSitemap extends Command
{
protected $signature = 'sitemap:generate';
protected $description = 'Generate the sitemap';
public function handle()
{
// Initialize XML string
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// Generate sitemap URLs dynamically
$products = \App\Models\Product::all();
foreach ($products as $product) {
$xml .= '<url>';
$xml .= '<loc>' . URL::to('/product/' . $product->id) . '</loc>';
$xml .= '<lastmod>' . $product->updated_at->tz('UTC')->toAtomString() . '</lastmod>';
$xml .= '<changefreq>daily</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
// Save the XML to the sitemap.xml file
File::put(public_path('sitemap.xml'), $xml);
$this->info('Sitemap generated successfully.');
}
}
Step 3: Register the Sitemap Command
To make the sitemap:generate
command accessible, we need to register it in the app/Console/Kernel.php
file. Open the file and add the following code to the commands
property:
protected $commands = [
Commands\GenerateSitemap::class,
];
Step 4: Generate the Sitemap
To generate the sitemap, simply run the command php artisan sitemap:generate
in your terminal. This will generate the sitemap XML file in the public
directory of your Laravel project.
Note : Remember to adjust the code according to your specific database structure and requirements.