Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 8 Generate Dynamic XML Sitemap

Laravel 8 Generate Dynamic XML Sitemap

Posted on September 29, 2020December 15, 2022 By XpertPhp No Comments on Laravel 8 Generate Dynamic XML Sitemap

We would like to share with you how to generate dynamic XML sitemap in laravel 8(Laravel 8 Generate Dynamic XML Sitemap). we will see scratch step by step generate dynamic XML sitemap in laravel 8.

A sitemap is described to navigate the website. users can easily search to navigate the site by sitemap. it is creating in the XML coding.

A sitemap provides information about our site and google can easily read XML files and crawl our site.

We have given below example of how to generate 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.

So you can follow the below steps.

Overview

Step 1: Install Laravel 8

Step 2: Create Routes

Step 3: Create a Controller

Step 4: Create a Model

Step 5: Create Blade File

Step 6: Run Our Laravel Application

Step 1: Install Laravel 8

We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel8_sitemap

Step 2: Create Routes

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
use App\Http\Controllers\SitemapController;
 
/*
|--------------------------------------------------------------------------
| 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::class, 'index']);
Route::get('sitemap.xml/article',[SitemapController::class, 'articles']);
Route::get('sitemap.xml/category',[SitemapController::class, 'categories']);
?>

Step 3: Create a Controller

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

1
php artisan make:controller SitemapController

app/Http/Controllers/SitemapController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\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');
   }
}
?>
See also  laravel eloquent WhereMonth query example

Step 4: Create Blade File

Finally, We will create 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

1
2
3
4
5
6
7
8
9
10
<?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

1
2
3
4
5
6
7
8
9
10
11
<?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

1
2
3
4
5
6
7
8
9
10
11
<?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
We can start the server and run this example using the below command.

1
php artisan serve

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

1
2
3
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

Laravel Tags:Laravel 8, laravel 8 create sitemap, laravel create sitemap

Post navigation

Previous Post: Laravel 8 Ajax Pagination Example Using Jquery
Next Post: Laravel 8 Generate RSS Feed Tutorial With Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com