How to generate dynamic XML sitemap in laravel 7 – onlinecode

How to generate dynamic XML sitemap in laravel 7 – onlinecode

In this post we will give you information about How to generate dynamic XML sitemap in laravel 7 – onlinecode. Hear we will give you detail about How to generate dynamic XML sitemap in laravel 7 – onlinecodeAnd how to use it also give you demo for it if it is necessary.

We would like to share with you how to generate dynamic XML sitemap in laravel 7. we will see scratch step by step generate dynamic XML sitemap in laravel 7.

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 7

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 7

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

composer create-project --prefer-dist laravel/laravel laravel7_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

Here 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 AppHttpControllers;
use IlluminateHttpRequest;
use AppPost;
use AppCategory;
  
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

Finally, 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 ApplicationWe can 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.



Please follow and like us:

Hope this code and post will helped you for implement How to generate dynamic XML sitemap in laravel 7 – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +    =  16

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US