• Hello, Visitor
  • Monday, March 17, 2025
Search Result
Home » More » SEO » Generate Laravel Dynamic Sitemap - SEO

Generate Laravel Dynamic Sitemap - SEO

For every website sitemap is most important parts of off-page SEO. Sitemap means or sitemap call a internal overview of a website.As you are reading this article here, most probably you are looking or want to how to make a dynamic sitemap for your laravel website.You will find lot’s of package about laravel sitemap but here I’ll introduce you how you can make a laravel sitemap for your website SEO without using any laravel package.It’s very easy steps that I’m going to show you.I'll show you step by step how you can make your laravel website sitemap easily. Let's start.


3 Steps For Create Laravel Sitemap:


  1. Define route

  2. Make a sitemap controller

  3. Make XML response for render sitemap.


    Step 01: Route Define 

    Make a simple get route in web.php file for handling the sitemap.xml request.


    Route::get('sitemap.xml','SitemapController@index')


    Step 02: Make a sitemap controller

    Make a sitemap controller by artisan command like php artisan make:controller SitemapController and write this code for making a sitemap.


    <?php namespace App\Http\Controllers;


    use App\Http\Requests;

    use App\Http\Controllers\Controller;


    use App\Post;

    use Illuminate\Http\Request;



    class SitemapController extends Controller

    {

        

        public function index(Request $r)

        {

           

            $posts = Post::orderBy('id','desc')->where('post_status', 'Publish')->get();


            return response()->view('sitemap', compact('posts'))

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


        }

    }


    Step 03: Make XML response for render sitemap

    Make a sitemap.blade.php view in resources


    <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

        @foreach ($posts as $post)

            <url>

                <loc>{{url($post->slug_url)}}</loc>

                <lastmod>{{ gmdate('Y-m-d\TH:i:s\Z',strtotime($post->updated_at)) }}</lastmod>

                <changefreq>daily</changefreq>

                <priority>0.6</priority>

            </url>

        @endforeach

    </urlset>


    Ofcourse, it's easy and simple. Your sitemap is ready for use. Just browse http://example.com/sitemap.xml


    Now your laravel website is ready for submit search engine like Google, Bing, Yandex, Yahoo etc to index their search result set. Hope this 3 steps simple dynamic sitemap creation with Laravel will help you to make your won. If this is helpful, then please share this post with others.


    Extra! 


    Make sure to go and submit your laravel sitemap in the Google Search Console as soon as you are done, and Google will then know it exists.



    Hope it will help you.


    Leave a Reply

    You must Login to add a comment