Laravel 8 Generate and Read XML File Tutorial Example

Laravel 8 Generate and Read XML File Tutorial Example

Laravel 8 create/generate 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;

 
<?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);

<?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.

php artisan make:controller SitemapController

Generate sitemap Index

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

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

<?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,

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:

<?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:

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.

Conclusion

In this article, you have learned how to create sitemap.xml in Laravel.

Recommended Laravel Tutorial

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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