Building the right Google Sitemap XML for Laravel 5.x in "4 short" steps only

What is a sitemap

If you are not familiar with a Sitemap, Google defines it as:

A sitemap is a file where you can list the web pages of your site to tell Google and other search engines about the organization of your site content. Search engine web crawlers like Googlebot read this file to more intelligently crawl your site.

They also outline the following reasons on why you need one:

  • Your site is really large. As a result, it’s more likely Google web crawlers might overlook crawling some of your new or recently updated pages.
  • Your site has a large archive of content pages that are isolated or well not linked to each other. If you site pages do not naturally reference each other, you can list them in a sitemap to ensure that Google does not overlook some of your pages.
  • Your site is new and has few external links to it. Googlebot and other web crawlers crawl the web by following links from one page to another. As a result, Google might not discover your pages if no other sites link to them.
  • Your site uses rich media content, is shown in Google News, or uses other sitemaps-compatible annotations. Google can take additional information from sitemaps into account for search, where appropriate.

Your site might not hit those criteria, but as I mentioned, I still believe it’s worth submitting one just to be safe.

 

Steps :

1- Create a folder in ~/resources/views named "sitemap"

2- got to the terminal and create a model named sitemapController 

php artisan make:controller SitemapController

*- after creating this file open it and paste the function below under the class name to be:

 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class SitemapController extends Controller
{
    public function index()
        {
          $post = Post::orderBy('updated_at', 'desc')->first();

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

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

    public function podcasts()
        {
            $podcast = Podcast::active()->orderBy('updated_at', 'desc')->get();
            return response()->view('sitemap.podcasts', [
                'podcasts' => $podcast,
            ])->header('Content-Type', 'text/xml');
        }
}
 

-->save it and move to the next step

3- Create 2 files in your sitemap folder ~/../views/sitemap/index.blade.php & ~/../views/sitemap/posts.blade.php

posts.blade.php

 

**- Post is the Controller of the articles or posts that you are using within your Laravel

index.blade.php

4- make sure to create a route for your sitemap and sitemap post, by editing ~/../routes/web.php add at the bottom the lines below 

Route::get('/sitemap', 'SitemapController@index');

Route::get('/sitemap/posts', 'SitemapController@posts');

 

Done.

Please refer to this post in case of using the information above.

Thank you 

 

 

By: Mutasem Elayyoub