Laravel 8 Create SEO Friendly XML Sitemap Example

A sitemap is a file where you provide information about the pages, videos, and other files on your site, and the relationships between them. Search engines like Google read this file to more intelligently crawl your site.

So here I would like to share with you how to create a dynamic XML sitemap in the Laravel 8 application. we will generate sitemap XML without using any plugin or anything in the Laravel application.

So in this tutorial, I will discuss sitemap in Laravel. We will see step by step how to create sitemap XML in Laravel 8. After completing this tutorial you can create a sitemap in your Laravel application and you can submit this sitemap in the google search console.

We can add sitemap XML in the Laravel 8 application. We can use spatielaravel sitemap package. But in this tutorial, I am going to use core Laravel code to create a dynamic XML sitemap in Laravel 8.

how to create sitemap xml in laravel


You can use a sitemap to provide information about specific types of content on your pages, including video and image content. For example:

  1. A sitemap video entry can specify the video running time, category, and age appropriateness rating.
  2. A sitemap image entry can include the image subject matter, type, and license.

Learn more from here Do i need a sitemap?


Read also : How to resize image in Laravel ?


Build and submit a sitemap

We can generate a sitemap from here but we need to create a sitemap using Laravel by our own coding style. Go to this link to learn more about sitemap and the rules of sitemap. When we submit our sitemap in our google web master without creating sitemap then its show us 404 error.

So removing this error first we have to create our using coding or will create from here. But in this tutorial, we will see how to create an XML sitemap using Laravel.

Example of a sitemap :


Benefits of having more than one sitemap

Let's say we have database tables for Articles, Categories, and Tags. We can create separate XML sitemaps for each of them which will be easily manageable and also be very clear to read for both humans and search engine robots. Then we will include all those 4 sitemaps into one index file and submit them to google, bing, or wherever we may please.

Getting Started

Let's create a new controller for our sitemaps.


Step 1: Create Controller

To make our sitemap controller, just run the below command

php artisan make:controller SitemapController 


Step 2: Create Require Route

Open your routes/web.php and paste the following code.


  /* Sitemap Route*/
  Route::get('/sitemap.xml', 'SitemapController@index')->name('sitemap.xml');
  Route::get('/sitemap.xml/article', 'SitemapController@articles');
  Route::get('/sitemap.xml/category', 'SitemapController@categories');
  Route::get('/sitemap.xml/tag', 'SitemapController@tags');
  Route::get('/sitemap.xml/subcategory', 'SitemapController@subcategories');


Step 3: Setup Controller 

Now open SitemapController controller and paste this below code to create dynamic xml sitemap in Laravel.


namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Model\User\Category;
use App\Model\User\Post;
use App\Model\User\Tag;
use App\Model\User\Subcategory;
use Illuminate\Http\Request;
class SitemapController extends Controller
    public function index() {
      $articles = Post::all()->first();
      $categories = Category::all()->first();
      $tags = Tag::all()->first();
      $subcategories = Subcategory::all()->first();
      return response()->view('sitemap.index', [
          'article' => $articles,
          'category' => $categories,
          'subcategory' => $subcategories,
          'tag' => $tags,
      ])->header('Content-Type', 'text/xml');

    public function articles() {
       $article = Post::latest()->get();
       return response()->view('sitemap.article', [
           'article' => $article,
       ])->header('Content-Type', 'text/xml');

   public function categories() {
       $category = Category::all();
       return response()->view('sitemap.category', [
           'category' => $category,
       ])->header('Content-Type', 'text/xml');
   public function subcategories() {
       $subcategory = Subcategory::all();
       return response()->view('sitemap.subcategory', [
           'subcategory' => $subcategory,
       ])->header('Content-Type', 'text/xml');

   public function tags() {
       $tag = Tag::all();
       return response()->view('sitemap.tag', [
           'tag' => $tag,
       ])->header('Content-Type', 'text/xml');


Now make a folder inside resources/views/sitemap/index.php and paste the following code:



Now if you visit this slug you will see below the code:


Note: Just replace by your website slug

Now just print all the sitemap data like the below code inside  resources/views/sitemap/article.php and paste the following code



Now just print all the sitemap data like the below code inside resources/views/sitemap/category.php and paste the following code



Now just print all the sitemap data like the below code inside resources/views/sitemap/subcategory.php and paste the following code



Now just print all the sitemap data like the below code inside resources/views/sitemap/tag.php and paste the following code



Read also: RSS Feed Example in Laravel Without Packages


Now just visit those following slug to justify our sitemap content



Hope it can help you. If you have found any errors from this code, please share it with me in the comment box.


#laravel #sitemap #dynamic-sitemap-in-laravel #xml #dynamic-xml-sitemap