CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

22 February, 2021

Laravel 8 Generate and Read XML File Tutorial Example

 Programing Coderfunda     February 22, 2021     Laravel, laravel-tutorial     No comments   

 Laravel 8 Generate and Read XML File Tutorial Example


Laravel 8 create and read sitemap xml file. In this tutorial you will learn how to create search engine-friendly sitemap.xml and read it in laravel. This tutorial will show you each thing step by step for creating and reading a sitemap in laravel 8 app.

Since everyone is developing websites either for their businesses or organization. Search engines have been providing support with marketing. and One of the recommendations they give is creating a sitemap to aid with indexing the business or personal website.

Today, the importance of sitemaps for website owners is controversial. you’re not going to lose anything by sparing a little time to create one. and this is made easy by existing tools such as putting together a sitemap in Laravel. 

Sitemap

Sitemap concept is defined as a file developed with the primary purpose of listing the website pages, which will enable search engines such as Google, Mozilla, Internet Explorer, and Opera Mini to learn about the organizations of the content you have in your site.

Search engines have web crawlers that interpret these files helping the search engine crawl your site more intelligently, for example, the Googlebot. 

Sitemaps provide the valued metadata that is linked to your pages, as listed in the sitemap created. Metadata is said to be the information regarding the webpage, this may be information such as the last webpage update, the frequency at which the page is changed, and how significant the page is to add URLs in the website. 

Sitemaps can be created manually or through an automatic solution. The manual process involves creating their own sitemap.xm1 file and carefully mapping the URLs of your website, or one can use available online tools that help in generating a sitemap by merely entering the URL of your site, where the tool will make the sitemap for you to upload to your server. 

Why you need a sitemap 

  • There is a possibility of Google web crawlers to assume some or all of your recently updated or new pages while crawling.
  • Google gives the size of the site as one of the reasons why this is possible and hence the need for a sitemap,
  • Your website has too many pages that are not linked to each other or are isolated. This means they are not referencing each other and by listing these files in a sitemap decreases the chance of Google not overlooking these pages, 
  • Web crawlers such as Googlebot crawl your site using links from one page to another. Thus, if your site has scarce external links to it and it’s new, these crawlers might not discover it unless they are listed,
  • If your website has rich broadcasting material, is available in google bulletins, or makes use of other sitemaps-likeminded remarks, web crawlers can source out extra information from that which is listed in the sitemaps. 

Sitemap Procedure 

Since we are interested in the XML sitemap, this format aught to; use UTF-8 encoding, start with <urlset> tag for opening and end with <urlset> tag for closing, should specify the protocol standard (namespace) that is in the <urlset> tag, there should be a <url> entered for the existing URL as parent XML tag and you should contain a <loc> child entry for the <url> parent tag. The supplementary tags are voluntary and vary from search engine to the other. 

Thus, a basic sample embracing a single url should look like this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
 
      <loc>//www.tutsmake.com/</loc>
 
      <lastmod>2005-01-01</lastmod>
 
      <changefreq>monthly</changefreq>
 
      <priority>0.8</priority>
   </url>
</urlset>

It is an XML file holding you’re <url> for every page that exists in your site. A single XML file has the ability to hold up to fifty thousand archives, and you are given permission to separate them in order to use the index file to direct to the others. The above is outline as below; (let our website be tutsmake.com for the purpose of this article tutorial);

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>http:// tutsmake.com /sitemap1.xml.gz</loc>
      <lastmod>2004-10-01T18:23:17+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http:// tutsmake.com /sitemap2.xml.gz</loc>
      <lastmod>2005-01-01</lastmod>
   </sitemap>
</sitemapindex>

Like the previous example, each <loc> directs to a folder that it has included in the<url> items.

Let’s follow the below steps and create search engine friendly sitemap.xml in laravel:

Building a Sitemap Controller

Assuming we are building a sitemap with four primary sections that are the goods categories, purchases, FAQ, and an inquiry. Each will have their individual file and will get index direct to them. First, we generate a sitemap controller.

1
php artisan make:controller SitemapController

Generate sitemap Index

The aim here is to create an index method, which should create the needed XML.  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public function index()
{
  $product= Product::all()->first();
  $faq= FAQ::all () ->first();
  $inquiry = inquiry::all () ->first();
 
 
  return response()->view('sitemap.index', [
      'product' => $product,
      'FAQ' => $faq,
      'Inquiry' => $inquiry,
 
  ])->header('Content-Type', 'text/xml');
}

All the four categories, goods, purchases, FAQ, and inquiry, are vital for generating the timestamp of the last modified. This will inform the web crawlers in case of new content.

The sitemap.xml file

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
 
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>https:// tutsmake.com /sitemap/products</loc>
    </sitemap>
    <sitemap>
        <loc>https:// tutsmake.com /sitemap/FAQ</loc>
    </sitemap>
    </sitemap>
  <loc>https:// tutsmake.com /sitemap/Inquiry</loc>
 
</sitemapindex>

The next step is generating URL for the existing files, this will mean, the controller getting four methods which are similar as illustrated below,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function product()
{
    $products= Product::latest()->get();
    return response ()->view ('sitemap.product', [
        'products' => $products,
    ])->header ('Content-Type', 'text/xml');
}
 
public function faqs()
{
    $FAQ = FAQt::active()->orderBy('updated_at', 'desc')->get();
    return response()->view('sitemap.FAQ', [
        'FAQ' => $FAQ,
    ])->header('Content-Type', 'text/xml');
}
 
public function inquiry()
{
    $FAQ = inquiry ::active()->orderBy('updated_at', 'desc')->get();
    return response()->view('sitemap.inquiry ', [
        'inquiry ' => $ inquiry,
    ])->header('Content-Type', 'text/xml');
}

Now we need to create product.blade.php inside the folder name sitemap and put the below code into your

Than Your sitemap/product.blade.php should look like this:

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 ($prducts as $pro)
        <url>
            <loc>https://tutsmake.com/{{ $pro->uri }}</loc>
            <lastmod>{{ $post->publishes_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.6</priority>
        </url>
    @endforeach
</urlset>

Next step would be to place and duplicate each of your sections, it should be ready upon adding your routes. For example, from the above it will look like:

1
2
3
4
Route::get('/sitemap.xml', 'SitemapController@index');
Route::get('/sitemap.xml/products', 'SitemapController@product');
Route::get('/sitemap.xml/FAQ', 'SitemapController@FAQ');
Route::get('/sitemap.xml/Inquiry', 'SitemapController@inquiry);

Close by informing the search engine crawlers of the protocol location. This can be achieved through using the submission interface, sending an HTTP request, or by stipulating the location/position in your website’s robot’s txt file.  Creating your own sitemap should not be difficult with Laravel.

by: tutsmake.com

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Laravel 10.4 Released The Laravel team released 10.4 with a File::json() method, converting existing HasMany relationships to a HasOne relationship, a new test respon… Read More
  • Pest Architecture Plugin With the release of PestPHP v2, we can now test the architecture of our applications. In this tutorial, we will walk through how to use this plu… Read More
  • How to choose between APM and an end-to-end monitoring tool There was a time when APM was all anyone needed. Older monolithic web services were monitored with application performance monitoring (APM) tool… Read More
  • Laravel FFMpeg toolsThis package allows you to build FFMpeg strings in a fluent, easily maintainable way that feels familiar to php and Laravel devs. The post Laravel … Read More
  • Dispatcher – Laravel scheduled tasks Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don’t need to touch th… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Auto-translate Application Strings with Laratext - 5/16/2025
  • Simplify Factory Associations with Laravel's UseFactory Attribute - 5/13/2025
  • Improved Installation and Frontend Hooks in Laravel Echo 2.1 - 5/15/2025
  • Filter Model Attributes with Laravel's New except() Method - 5/13/2025
  • Arr::from() Method in Laravel 12.14 - 5/14/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda