HomeLaravel

Create a Dynamic Sitemap XML in Laravel without installing packages

Create a Dynamic Sitemap XML in Laravel without installing packages
Like Tweet Pin it Share Share Email

You have built a great application in Laravel and now you are searching for creating a dynamic sitemap for SEO. With the help of me, you can create a dynamic sitemap in your application for SEO. Google has introduced a protocol so a user can publish all the pages list. you can read more on Google Sitemap here.

Step 1: Create a sitemap controller

To generate a sitemap you need to create a Sitemap Controller using command. Please use the below command to generate it.

php artisan make:controller SitemapController

The generated Sitemap Controller will look like below. Please find it under app/Http/Controllers folder path

<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 class SitemapController extends Controller
{
	//
}

Step 2: Create routes

After creating controller we need to add URL structure in the route.php file. You can find it under /routes/ folder. In my demo website, I have 6 types of pages so I’m creating 6 routs rules. You can create rules based upon your pages type.

/*SITE MAP */
Route::get('/sitemap-index.xml', 'SitemapController@index');
Route::get('/sitemap-business-listing.xml', 'SitemapController@business');
Route::get('/sitemap-pages.xml', 'SitemapController@pages');
Route::get('/sitemap-city.xml', 'SitemapController@cities');
Route::get('/sitemap-state.xml', 'SitemapController@states');
Route::get('/sitemap-review.xml', 'SitemapController@review');
Route::get('/sitemap-business-category.xml', 'SitemapController@listingCategory');

These are URLs which I want to add to the google. You can change as per your requirement. I have created master sitemap file sitemap-index.xml which will inform Google to find all the pages’ sitemap links in it.

Step 3: Create a sitemap View

We have added a URL in the route with the function name. So add what you want to be displayed in the view files. sitemap-index.xml is our main file which is called by index() function.

public function index()
{
return response()->view('sitemap.index')->header('Content-Type', 'text/xml');
}

We are not adding any data from models into views for time being as we have to define models in the controller. Please copy the below code under your index function.

public function business()
{
	return response()->view('sitemap.business')->header('Content-Type', 'text/xml');
}

public function pages()
{
	return response()->view('sitemap.pages')->header('Content-Type', 'text/xml');
}

public function cities()
{
	return response()->view('sitemap.cities')->header('Content-Type', 'text/xml');
}

public function states()
{
	return response()->view('sitemap.state')->header('Content-Type', 'text/xml');
}

public function review()
{
	return response()->view('sitemap.review')->header('Content-Type', 'text/xml');
}

public function listingCategory()
{
	return response()->view('sitemap.business_category')->header('Content-Type', 'text/xml');
}

Step 4: Adding Models in the controller

Now you can add your all models under the controller and collect all the required data to display on the sitemap pages. after doing changes at my side, here is new SitemapController.php file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Model\admin\City;
use App\Model\admin\Category;
use App\Model\admin\Listing;
use App\Model\Review;

class SitemapController extends Controller
{
	public function index()
	{
		return response()->view('sitemap.index')->header('Content-Type', 'text/xml');
	}
	public function business()
	{
		$listing =  Listing::orderBy('updated_at', 'desc')->get();
		return response()->view('sitemap.business', ['listing' => $listing])->header('Content-Type', 'text/xml');
	}
	
	public function cities()
	{
		$cities =  City::all();
		return response()->view('sitemap.cities', ['cities' => $cities])->header('Content-Type', 'text/xml');
	}
	public function states()
	{
		$states =  State::all();
		return response()->view('sitemap.state', ['states' => $states])->header('Content-Type', 'text/xml');
	}

public function pages()
	{
		return response()->view('web.sitemap.pages')->header('Content-Type', 'text/xml');
	}

	
	public function review()
	{
		$reviews =  Review::orderBy('updated_at', 'desc')->get();
		return response()->view('sitemap.review', ['reviews' => $reviews])->header('Content-Type', 'text/xml');
	}
	public function listingCategory()
	{
		$categories =  Category::all();
		return response()->view('sitemap.business_category', ['categories' => $categories])->header('Content-Type', 'text/xml');
	}
}

Step 5: Create a view

Create a sitemap folder under your view and create a required page as per your requirement. Now I have created below blade pages in my sitemap folder.

sitemap/index
sitemap/business_category.blade.php
sitemap/review.blade.php
sitemap/state.blade.php
sitemap/cities.blade.php
sitemap/business.blade.php

We want our result in XML format so we have to add XML code in our view blade files.

index.blade.php

In index.blade.php we are going to display all sitemap pages link. So I have added as per my requirements. Please copy below code and paste in your index.blade.php file with your requirement.

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>{{url('sitemap-business-listing.xml')}}</loc>
    </sitemap>
    <sitemap>
        <loc>{{url('sitemap-pages.xml')}}</loc>
    </sitemap>
    <sitemap>
        <loc>{{url('sitemap-city.xml')}}</loc>
    </sitemap>
    <sitemap>
        <loc>{{url('sitemap-state.xml')}}</loc>
    </sitemap>
    <sitemap>
        <loc>{{url('sitemap-review.xml')}}</loc>
    </sitemap>
     <sitemap>
        <loc>{{url('sitemap-business-category.xml')}}</loc>
    </sitemap>
    
</sitemapindex>

Now it’s time to add code into the rest of the blade pages. after adding code into the page, please open sitemap-index.xml in your browser and you will see below output.

http://localhost:81/lavarel/sitemap-index.xml output
Sitemap index
Sitemap index

 

business.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
@foreach ($listing as $listing)
@php
$list_url = url('/').$listing->url;
@endphp
<url>
	<loc>{{$list_url}}</loc>
	<lastmod>{{ $listing->updated_at->tz('UTC')->toAtomString() }}</lastmod>
	<priority>0.9</priority>
</url>
@endforeach
</urlset>
http://localhost:81/lavarel/sitemap-business.xml output
sitemap
Sitemap Business

 

business_category.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
@foreach ($categories as $category)
@php
$url = url('categories');
@endphp
<url>
	<loc>{{$url}}</loc>
	<lastmod>{{ $category->updated_at->tz('UTC')->toAtomString() }}</lastmod>
	<priority>0.9</priority>
</url>
@endforeach
</urlset>
http://localhost:81/laravel/sitemap-business-category.xml output

sitemap-business-category

 

city.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
@foreach ($cities as $city)
@php
$url = url('cities');
@endphp
<url>
	<loc>{{$url}}</loc>
	<lastmod>{{ $city->updated_at->tz('UTC')->toAtomString() }}</lastmod>
	<changefreq>weekly</changefreq>
	<priority>0.9</priority>
</url>
@endforeach
</urlset>
http://localhost:81/laravel/sitemap-city.xml output

 

sitemap city
sitemap city

page.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
	<loc>{{url('/')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>1.00</priority>
</url>
<url>
	<loc>{{url('listing')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>	
</url>
<url>
	<loc>{{url('login')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('register')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('contact')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('about')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('reviews')}}</loc>
	<lastmod>2019-10-01T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('cookie-policy')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('privacy-policy')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>
<url>
	<loc>{{url('terms-and-condition')}}</loc>
	<lastmod>2019-09-26T08:11:31+00:00</lastmod>
	<priority>0.80</priority>
</url>


</urlset>
http://localhost:81/laravel/sitemap-pages.xml output

 

business-pages

review.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
@foreach ($reviews as $review)
@php
$url = url('review');
@endphp
<url>
	<loc>{{$url}}</loc>
	<lastmod>{{ $review->updated_at->tz('UTC')->toAtomString() }}</lastmod>
	<changefreq>weekly</changefreq>
	<priority>0.9</priority>
</url>
@endforeach
</urlset>
http://localhost:81/laravel/sitemap-review.xml output

 

Business Review
Business Review

state.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
@foreach ($states as $state)
@php
$url = url('state');
@endphp
<url>
	<loc>{{$url}}</loc>
	<lastmod>{{ $state->updated_at->tz('UTC')->toAtomString() }}</lastmod>
	<changefreq>weekly</changefreq>
	<priority>0.9</priority>
</url>
@endforeach
</urlset>
http://localhost:81/laravel/sitemap-state.xml output
sitemap state
sitemap state

 

Step 6: Submit sitemap-index.xml into Google Search Console.

We have created the main index sitemap file i.e sitemap-index.xml and we need to submit this file only into the Google Search Console. Google will automatically crawl the remaining sitemap as in the index sitemap we added <pre><loc></pre> which tells Google to find a sitemap location.

 

 

Thanks for reading our article. What Next?

Comments (0)

Leave a Reply

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

two × 5 =