The dynamic sitemap.xml file creation in the Laravel application will be covered in this tutorial on Laravel 10. Furthermore, we also like to explain how to read the Laravel application’s sitemap xml file.
You can use the spatie/laravel-sitemap package to construct an XML sitemap in a Laravel application. The stepsāpackage installation, setup, and sitemap generationāare listed below.
Step 1: Install the Package
First, install the spatie/laravel-sitemap
package via Composer:
composer require spatie/laravel-sitemap
Step 2: Publish the Configuration File
If you want to customize the default configuration of the package, you can publish the configuration file:
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider"
Step 3: Set Up URL Tracking
If you want to include URLs based on daily activity or clicks, you need to track these activities. Here’s how you can do it:
Create a Model and Migration for URL Clicks
php artisan make:model UrlClick -m
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UrlClick extends Model
{
use HasFactory;
protected $fillable = ['url'];
}
Step 4: Define the Migration
In the migration file database/migrations/yyyy_mm_dd_create_url_clicks_table.php
, define the table structure:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUrlClicksTable extends Migration
{
public function up()
{
Schema::create('url_clicks', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('url_clicks');
}
}
Step 5: Run the migration:
php artisan migrate
Step 7: Track URL Clicks
Create a middleware to track URL clicks. Generate the middleware:
php artisan make:middleware TrackUrlClick
In app/Http/Middleware/TrackUrlClick.php
:
namespace App\Http\Middleware;
use Closure;
use App\Models\UrlClick;
class TrackUrlClick
{
public function handle($request, Closure $next)
{
if ($request->isMethod('get')) {
UrlClick::create(['url' => $request->path()]);
}
return $next($request);
}
}
Step 8: Register the middleware in app/Http/Kernel.php
:
protected $middlewareGroups = [
'web' => [
// other middlewares
\App\Http\Middleware\TrackUrlClick::class,
],
];
Step 9: Create the Sitemap Route
Create a route to generate the sitemap based on the tracked URL clicks.
In routes/web.php
:
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Log;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
use App\Models\UrlClick;
use Carbon\Carbon;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/sitemap', function () {
$sitemap = Sitemap::create();
// Add URLs clicked in the last 24 hours
$urlClicks = UrlClick::where('created_at', '>=', Carbon::now()->subDay())->get();
foreach ($urlClicks as $urlClick) {
$sitemap->add(Url::create($urlClick->url)
->setLastModificationDate($urlClick->created_at)
->setPriority(0.7));
}
return $sitemap->toResponse(request());
});
Step 10: Access the Sitemap
Visit the URL /sitemap
in your browser to see the generated sitemap. For example, if your application is running on http://localhost:8000
, go to http://localhost:8000/sitemap
.
This is my local setup with the result
