In this tutorial you will learn about the How to Generate sitemap.xml file in Laravel and its application with practical example.
In this How to Generate sitemap.xml file in Laravel tutorial I will show you how to create sitemap.xml file in laravel application. In this tutorial you will learn to create sitemap.xml file for you laravel website. Sitemap.xml file is important for any website, it is used by search engines for indexing the website. In this step by step guide I will demonstrate you how to create search engine or seo friendly sitemap.xml for laravel websites.
How to Generate sitemap.xml file in Laravel
In this tutorial you will learn to create search engine friendly sitemap.xml in laravel. Here I will demonstrate you step by step to create a sitemap.xml file in laravel.
Create a Sitemap Controller
In this step we will create SitemapController using following artisan command:
1 |
php artisan make:controller SitemapController |
Now, we will create a index method to generate 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'); } |
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://yoursite.com /sitemap/products</loc> </sitemap> <sitemap> <loc>https://yoursite.com /sitemap/FAQ</loc> </sitemap> </sitemap> <loc>https://yoursite.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 will create a product.blade.php inside the folder name sitemap and put the following code in it. 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://yoursite.com/{{ $pro->uri }}</loc> <lastmod>{{ $post->publishes_at->tz('UTC')->toAtomString() }}</lastmod> <changefreq>weekly</changefreq> <priority>0.6</priority> </url> @endforeach </urlset> |
NOw, we will define some routes as following
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); |