Hi 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:
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
@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: How to Send an Email on Error Exceptions in Laravel 9?http://localhost:8000/sitemap.xml
Output:
I hope it can help you...