LaravelTutorials

Create Sitemap XML in Laravel 8

Divi WordPress Theme

You will learn create sitemap in laravel. This tutorial will give you simple example of sitemap generator in laravel.

Simply step by step laravel sitemap example Here you will learn sitemap.xml in laravel. So, let’s follow few step to create example of sitemap in laravel.

Google gives the size of the site as one of the reasons why this is possible and need for a sitemap, Your website has too many pages that are not linked to each other or are isolated.

This means they are not referencing each other and by listing these files in a sitemap decreases the chance of Google not overlooking these pages, You will follow bellow step by step to create sitemap in your laravel app.

The Most Customizable WordPress Theme – The7

Step 1 : Create Route
routes/web.php

Route::get('/sitemap.xml', 'SiteMapController@index');

Step 2 : Create Controller and Method
app/http/controllers/SiteMapController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class SiteMapController extends Controller
{
    public function index()
	{
	  $posts= Post::latest()->get();
	 
	  return response()->view('sitemap.index', [
	      'posts' => $posts,
	  ])->header('Content-Type', 'text/xml');
	}

}

Step 3 : Create View File
resources/views/sitemap/index.blade.php

<?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>https://creepes.com/{{ $post->title }}</loc>
            <lastmod>{{ $post->created_at }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.6</priority>
        </url>
    @endforeach
</urlset>

Step 4 : Run Your Application

php artisan serve

Step 5 : Open following link

http://127.0.0.1:8000/sitemap.xml

You can check and confirm this. if have any problem please contact me.

thank you for follow this tutorial…

Khandoker Hirok

I'm an expert web developer over 6 years experience. I can fix bugs, customize and makes website and Software as per your requirements with Wordpress, PHP, Laravel and Codeigniter. My Fiverr: https://fiverr.com/hirokbd

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button