Laravel 9 Generate Sitemap XML File Tutorial Example
By Hardik Savani June 3, 2022 Category : LaravelVideo Player is loading.PauseUnmuteLoaded: 0.00%Seek to live, currently behind liveLIVERemaining Time -49:46FullscreenHi Dev,
Today, laravel 9 generate sitemap is our main topic. I’m going to show you about how to create dynamic xml sitemap in laravel 9. you will learn laravel 9 sitemap xml. I would like to share with you laravel 9 sitemap dynamic generate. You just need to some step to done laravel 9 generate sitemap file.
A sitemap is an XML File that has a list of URLs of your website. You can submit a sitemap file to the webmaster tool to inform the search engine. So Sitemap is very important for every website SEO.
In this example, we will create posts table with title, slug and body. Then we will create a factory for dummy posts. Then after we will generate an XML file and list all URLs for posts. It's a very basic example. so let's follow and you will get a sitemap file for your website and submit it to the webmaster's tool.
Let's follow below steps:
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
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
<?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:
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.
Next, copy below code and update PostFactory.php file.
database/factories/PostFactory.php
Then simply run tinker command and create dummy posts.
Step 4: Create Route
In this step, we will create one route sitemap.xml. so let's add it.
routes/web.php
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
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
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:
Now, Go to your web browser, type the given URL and view the app output:
<?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' ];}
php artisan make:factory 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 */ public function definition() { return [ 'title' => $this->faker->text(), 'slug' => Str::slug($this->faker->text()), 'body' => $this->faker->paragraph() ]; }}
<?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']);
<?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'); }}