How to Automatically Generate Sitemap in Laravel Laravel8 , Laravel9 ?

Spread the love

In this tutorial, I will guide you on how to generate a dynamic XML sitemap in Laravel. We’ll cover the step-by-step process of creating the sitemap and explain it in a simple manner. You’ll learn how to generate a sitemap for your Laravel application, which can be used with versions 6, 7, 8, 9, and 10.

 

A sitemap is a crucial XML file containing a list of URLs from your website. By submitting the sitemap to webmaster tools, you inform search engines about the structure of your site, making it essential for SEO purposes.

 

In our example, we’ll start by creating a “posts” table with fields like title, slug, and body. Next, we’ll generate a factory to create dummy posts. After that, we’ll proceed to generate the XML file and list all the URLs for the posts. This example serves as a basic guide to creating your sitemap file, which you can then submit to webmaster tools for search engine indexing.

 

Let’s get started by following the steps outlined below:


Step 1: Install Laravel

This step is optional, but if you haven’t created the Laravel app yet, you can proceed by executing the following command:

composer create-project laravel/laravel example-app

Step 2: Create Post Migration and Model

In this step, we will create migration and model. So let’s run below command to create posts table.

 

php artisan make:migration create_posts_table

 

Next, simple update below code to migration file.

 

database/migrations/create_posts_table.php

 

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->string('slug');

$table->text('body');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

};

 

Then run created new migration with below command:

php artisan migrate

 

Now, run below command to create Post model.

 

php artisan make:model Post

 

Then update following code to Post model.

 

app/Models/Post.php

 namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Post extends Model

{

use HasFactory;

protected $fillable = [

'title', 'slug', 'body'

];

}

 

Step 3: Create Post Factory

 

In this step, we will create Post factory class and generate dummy records using tinker command. so let’s run below command to create post factory.

 

php artisan make:factory PostFactory

 

Next, copy below code and update PostFactory.php file.

 

database/factories/PostFactory.php

 namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

use App\Models\Post;

use Illuminate\Support\Str;

/**

* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>

*/

class PostFactory extends Factory

{

/**

* The name of the factory's corresponding model.

*

* @var string

*/

protected $model = Post::class;

 

/**

* Define the model's default state.

*

* @return array<string, mixed="" style="box-sizing: border-box; font-family: "crimson text";"></string,>

*/

public function definition()

{

return [

'title' => $this->faker->text(),

'slug' => Str::slug($this->faker->text()),

'body' => $this->faker->paragraph()

];

}

}

 

 

Then simply run tinker command and create dummy posts.

 

php artisan tinker

App\Models\Post::factory()->count(30)->create();

 

Step 4: Create Route

 

In this step, we will create one route sitemap.xml. so let’s add it.

 

routes/web.php

 use Illuminate\Support\Facades\Route;

use App\Http\Controllers\SitemapController;

/*

|--------------------------------------------------------------------------

| 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.xml', [SitemapController::class, 'index']);

| 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.xml'[SitemapController::class, ‘index’]);

 

Step 5: Create Controller

 

In this step, we have to create new controller as SitemapController with index(). we will get all posts and pass to blade file. we will return response as xml file. so let’s update follow code:

 

app/Http/Controllers/SitemapController.php

 

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class SitemapController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index($value='')

{

$posts = Post::latest()->get();

return response()->view('sitemap', [

'posts' => $posts

])->header('Content-Type', 'text/xml');

}

}

Step 6: Create View File

 

In Last step, let’s create sitemap.blade.php for display all posts and put following code:

 

resources/views/sitemap.blade.php

 

'; ?>

 

@foreach ($posts as $post)

{{ url('/') }}/post/{{ $post->slug }}

{{ $post->created_at->tz('UTC')->toAtomString() }}

daily

0.8

@endforeach

 

Run Laravel App:

 

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

 

php artisan serve

 

Now, Go to your web browser, type the given URL and view the app output:

 

Read Also: Laravel Pagination Pretty URL Example

 

http://localhost:8000/sitemap.xml

Demo:

Download Links

Scroll to Top