How to Generate XML Sitemap Without Using Package

In this article, I’m going to share how to create an XML sitemap manually. Let’s assume that we have a model named Article for table articles. We’re going to generate sitemap for the articles table. Let’s get started:

Table of Contents

  1. Create a Controller
  2. Create a View
  3. Make Route & Test

Create a Controller

I’m creating a controller called SitemapController. Run this command to create the controller:

php artisan make:controller SitemapController

After creating the controller, open it and paste the below code:


namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

class SitemapController extends Controller
    public function articles()
        $articles = Article::all();

        return response()->view('sitemap-articles', compact('articles'))->header('Content-Type', 'text/xml');

Create a View

Go to resources>views folder and make a view file named sitemap-articles.blade.php. Then open the file & paste this code:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="">
    @foreach ($articles as $article)
            <loc> {{ url('') . '/' . $article->slug }}</loc>
            <lastmod>{{ $article->created_at->tz('UTC')->toAtomString() }}</lastmod>

Make Route & Test

Open web.php from the routes folder and make a route like:

Route::get('/sitemap-articles.xml', 'SitemapController@articles');

Now run the project and visit You’ll see the output like:

That’s all. Thanks for reading.

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.