Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home Laravel 9: How to Generate and Read Sitemap XML File

Laravel

Laravel 9: How to Generate and Read Sitemap XML File

JustinBy JustinMarch 11, 2022Updated:May 27, 2022No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




First question is in your mind is why we need this Sitemap?

let be describe you first, Sitemap ideally is a file that ends with a .xml extension; it is a simple file that contains the important website pages and allows the webmaster to inform search engines what pages are available for crawling.

This sitemap file is not just limited to laravel only, no matter what technology you are using but make sure you must generate and add a sitemap xml file to inform search engines.

Here we all know about SEO, and this is not just a keyword; rather, this word decides the popularity of web applications. SEO decides the ranking of your site on the search engines, That is why we need sitemap for our website.

Sitemap design like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>https://www.readytocode.net/</loc>
      <lastmod>2022-03-11</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset>

Describe :

  1. It starts with an opening <urlset> tag relatively with ending </urlset>closing tag.
  2. The xmlns property used to define the namespace prefix inside the <urlset> tag.
  3. The xml contains url entry for every url that needs to be added in the Sitemap.
  4. The loc property is the child value of url which holds the web page url.
  5. The lastmod attribute is the child element of url, which reveals when was the page last modified.
  6. The changefreq and priority attributes are relative child values of the url property; these props let search engine about the crawling priority and update frequencies.

 

Now our Reading part is completed, lets jump on Coding .

Step 1.

Install a fresh Laravel project.

Step 2.

Attach with database.

Step 3.

Create some dummy data.

Step 4.

Create Controller for Sitemap and Route for this.

php artisan make:controller SitemapXml

Routes/web.php

Route::get('/sitemap.xml', [SitemapXml::class, 'index']);

 

Step 5.

Now we will add method index in controller file for the route which we connected.

public function index(){
        $urls = Post::all();
        return response()->view('sitemap', [
            'urls' => $urls
        ])->header('Content-Type', 'text/xml');
}

And also i will create a blade file with named sitemap.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($urls as $url)
        <url>
            <loc>{{ url('/') }}/{{ $url->slug }}</loc>
            <lastmod>{{date('Y-m-d', strtotime($url->updated_at))}}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.8</priority>
        </url>
    @endforeach
</urlset>

 

Step 6.

All done just run the project and open your new route URL,

php artisan serve

 

URL will be

http://127.0.0.1:8000/sitemap.xml

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

laravel google search console laravel sitemap laravel9 sitemap sitemap
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d