Laravel Sitemap | Create Dynamic XML Sitemap in Laravel 7

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 dynamic XML sitemap in Laravel 7 application. we will generate sitemap XML without using any plugin or anything in laravel application.

So in this tutorial i will discuss about sitemap in laravel. We will see step by step how to create sitemap xml in laravel 7. After completing this tutorial you can create sitemap in your laravel application and you can submit this sitemap in google search console.

We can add sitemap xml in laravel 7 application. We can use spatielaravel sitemap package. But in this tutorial i am going to use core laravel code to create dynamic xml sitemap in laravel 7.

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 sitemap from here but we need to create 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 a xml sitemap using Laravel.

Example of a sitemap :

Benefits of having more than one sitemap

Let's say we have database tables for ArticlesCategories and Tags. We can create seperate xml sitemaps for each of them which will be easily managable 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 to google, bing or wherever we may please.

Getting Started

Let's create a new controller for our sitemaps.

Step 1: Create Controller

For 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', '[email protected]')->name('sitemap.xml');
  Route::get('/sitemap.xml/article', '[email protected]');
  Route::get('/sitemap.xml/category', '[email protected]');
  Route::get('/sitemap.xml/tag', '[email protected]');
  Route::get('/sitemap.xml/subcategory', '[email protected]');

Step 2: Setup Controller 

Now open App\Http\Controllers\SitemapController

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  http://127.0.0.1:8000/sitemap.xml you will see below code

Note: Just replace http://127.0.0.1:8000/sitemap.xml/article by your website slug

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

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

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

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

Now just visit those following slug to justify our sitemap content

  1. http://127.0.0.1:8000/sitemap.xml
  2. http://127.0.0.1:8000/sitemap.xml/article
  3. http://127.0.0.1:8000/sitemap.xml/category
  4. http://127.0.0.1:8000/sitemap.xml/tag
  5. http://127.0.0.1:8000/sitemap.xml/subcategory

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

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website