How to Create or Generate Sitemap XML file in Laravel 8
Laravel 8 dynamic XML sitemap tutorial, we will talk about creating a dynamic sitemap.xml file in the laravel app. Moreover, we also like to describe how to read the sitemap xml file in the laravel application.
Sitemap Archetype:
Here is the logical structure of sitemap file.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<urlset>
<loc>https://www.fundaofwebit.com</loc>
<lastmod>2021-10-21T17:47:48+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
How to Generate and Read Sitemap XML File in Laravel 8:
Step 1: Install the Laravel 8 Project by following command:
$ composer create-project --prefer-dist laravel/laravel fundaBlog "8.0.*"
Step 2: Setup the Database & Schema string length:
Set up the database connection in your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=fundaofwebit
DB_USERNAME=username
DB_PASSWORD=password
Setup your database varchar length by Schema default String Length in the following path: app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
Step 3: Now, Lets create a Model and Migration in laravel 8 for Posts data..
Create a Model as Post as follows:
$ php artisan make:model Post
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $table = 'posts';
protected $fillable = [
'title',
'slug',
'description',
];
}
Create a Migration as follows:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable 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('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Step 4: Lets Add some posts or blog in your migrated table. (posts).
Lets Setup the code to Generate the Sitemap in laravel application
Step 5: Lets create a route to generate the sitemap.xml, so go to web.php file and paste the below code:
Route::get('/sitemap.xml', [App\Http\Controllers\SitemapXmlController::class, 'index']);
Step 6: create a controlled named SitemapXmlController.php as follows:
$ php artisan make:controoller SitemapXmlController
and create a function named index as given below:
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class SitemapXmlController extends Controller
{
public function index()
{
$posts = Post::all();
return response()->view('sitemap', [
'posts' => $posts,
])->header('Content-Type', 'text/xml');
}
}
Step 7: Now guys, we are going to display the Sitemap URL in laravel as given below: in the following path - resources/views/sitemap.blade.php
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://www.fundaofwebit.com</loc>
<lastmod>2021-10-21T17:47:48+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
@foreach ($posts as $post)
<url>
<loc>{{ url('/post/'.$post->slug) }}</loc>
<lastmod>{{ $post->created_at->tz('UTC')->toAtomString() }}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
@endforeach
</urlset>
Step 8: Lets serve the application by the following command:
$ php artisan serve
Thanks for reading..