Create dynamic XML sitemap in laravel - SEO

The sitemap of our website is one of the important parts of off-page SEO.  It's called the entire URL of our website. If you reached here to read this article,  that means you are looking for how to make a dynamic sitemap for your Laravel website.  There are several packages available for creating laravel sitemap but in this article, you will know that how you can create   a simple dynamic sitemap without using the Laravel sitemap package.  sitemap creation with Laravel is a very easy process with few steps. I will explain it step by step.

 

If you are looking for how to create a dynamic sitemap for your Laravel website? 

Here are 4 easy steps article for creating a dynamic XML sitemap for the Laravel website.

We will create a dynamic XML sitemap using below four steps

  1. Setup your laravel projects
  2. Define the route 
  3. Create a SitemapController
  4. Create XML response or view for render sitemap.


Step 01: Setup your laravel projects

I have already set up our laravel projects if you are beginners and want to know how to install  laravel you can go through:  Laravel 8,9 Installation from basic and changing the welcome page


Step 02: Define the route

You have to define a simple get the route in route > web.php file and call the controller into it.

        
        <?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('/', function () {
    return view('welcome');
});

Route::get('/sitemap.xml', [SitemapController::class, 'sitemap']);
        
    

Step 03: Create a SitemapController

You can simply create a SitemapController using artisan command 

php artisan make:controller SitemapController

and create a function sitemap for creating the sitemap.

here you can call your all different table data and compact them for accessing in your view file. I have created only a post table so I have a Post model.

        
        <?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Post;

class SitemapController extends Controller
{
    public function sitemap(){
        $blog_post = Post::where('is_published',1)->where('is_deleted',0)->get();
       
        return response()->view('frontend.sitemap',compact('blog_post'))->header('Content-Type','text/xml');
    }
}
        
    

Related Post

Step 4: Create XML response or view for render sitemap.

Now, create a sitemap.blade.php view file inside the resources>views folder and paste the below code. Note one thing we have returned a response with header Content-Type XML  so it is necessary to our view file should be formated as XML.


        
        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>https://abdultechhub.com/</loc>
        <lastmod>2022-11-03</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.00</priority>
    </url>

    @foreach ($blog_post as $post)
        <url>
            <loc>{{ url('blog/' . $post->slug) }}</loc>
            <lastmod>{{ gmdate('Y-m-d\TH:i:s\Z', strtotime($post->updated_at)) }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.80</priority>
        </url>
    @endforeach

</urlset>
        
    

Note: I hope you all have created the dynamic XML sitemap successfully if you are getting any errors you can comment below or contact me via the contact form.

Leave a comment