Generate Dynamic XML Sitemap With Example Laravel 8


This tutorial shares with you how to generate a dynamic XML sitemap in laravel 8. we will learn how to make a sitemap in PHP.

A sitemap is described to navigate the website. With the help of Sitemap, the user can easily go to any URL, the user has no problem in finding anyone URLs. it is a sitemap XML generator.

The sitemap provides information about our site and googles etc can easily read XML files and crawl our site and index fast.

This tutorial sees you learn a sitemap generator.

Given below example of how to generate a dynamic XML sitemap using laravel. Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file. learn Dynamic XML sitemap generator PHP script.

So you can follow the below steps. Then learn how to make a sitemap.

How To Generate Sitemap In Php | Dynamic XML Sitemap In Laravel 8
Step 1: Install Laravel 8

First of all, open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

composer create-project --prefer-dist laravel/laravel laravel8_sitemap
Step 2: Create Routes

Add the following route code in the “routes/web.php” file.

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/ 
Route::get('sitemap.xml','SitemapController@index');
Route::get('sitemap.xml/article','SitemapController@articles');
Route::get('sitemap.xml/category','SitemapController@categories');
?>
Step 3: Create A Controller

In this step, You can use the below command use to create a controller.

php artisan make:controller SitemapController


app/Http/Controllers/SitemapController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Category;
  
class SitemapController extends Controller
{
    public function index() {
      $articles = Post::all()->first();
      $categories = Category::all()->first();
      return response()->view('sitemap.index', [
          'article' => $articles,
          'category' => $categories
      ])->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');
   }
}
?>
Step 4: Create Blade File

We will create an index.blade.php, article.blade.php, and category.blade.php files in the “resources/views/sitemap/” folder directory and paste the below code.

resources/views/index.blade.php

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

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://127.0.0.1:8000/sitemap.xml/article</loc>
    </sitemap>
    <sitemap>
        <loc>http://127.0.0.1:8000/sitemap.xml/category</loc>
    </sitemap>
</sitemapindex>

resources/views/article.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($article as $content)
        <url>
            <loc>{{ url('/') }}article/{{ $content->slug }}</loc>
            <lastmod>{{ $content->created_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.9</priority>
        </url>
    @endforeach
</urlset>

resources/views/category.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($category as $category)
        <url>
            <loc>{{ url('/') }}article/category/{{ $category ->slug }}</loc>
            <lastmod>{{ $content->created_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.9</priority>
        </url>
    @endforeach
</urlset>
Step 5: Run Our Laravel Application

start the server and run this example using the below command.

php artisan serve


Now we will run our example using the below Url in the browser.

http://127.0.0.1:8000/sitemap.xml
http://127.0.0.1:8000/sitemap.xml/article
http://127.0.0.1:8000/sitemap.xml/category

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close