ホーム > Laravel > How to Create a Sitemap in Laravel 【SEO Measures】
Laravel

How to Create a Sitemap in Laravel 【SEO Measures】

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

Creating a sitemap is important for appearing in Google search results, and this article will discuss how to create a sitemap in Laravel.

The method we are about to follow is a DIY approach without using packages.

So, while there may be better methods out there, ultimately the result is a sitemap so there should be minimal issues, mostly related to performance like file writing and SQL, I think.

Maybe.

I would be happy to receive your feedback on Twitter.

⇨ How to Add lastmod to a Sitemap with Gatsby

⇨ Priority and changefreq are Not Needed in a Sitemap

Why Create a Sitemap?

A sitemap serves the purpose of informing Google's search algorithm "Here are the pages on this site."

Google recommends submitting a sitemap, and it is advisable to do so.

Blogs tend to do this regularly.

While Laravel has packages for creating sitemaps, we will proceed without using packages this time.

Testing Environment

Laravel 6

Why Not Use Packages

Packages can be difficult to work with when flexibility is needed, and it can become cumbersome to search through the documentation.

Due to the nature of sitemaps, it is necessary to create an accurate one, consistent with the canonical tag and requiring frequent comparison with the specifications of packages.

On the other hand, with this method, although the initial setup may be cumbersome (such as scheduling it for regular execution with cron and updating it daily), once the setup is done, it is easy to add, remove, or edit with code.

Whether it is a large site or a small one, I think ultimately it might be faster to create it manually.

Creating a Sitemap

Here is the process we will follow.

① Create a command to generate sitemap.xml.

② Return that file as a response.

That's the flow.

Let's get started!!!!!!!!

Creating the Command

Creating a command file

Run the command in the terminal.

php artisan make:command SitemapCommand

App/Console/Command/SitemapCommand.php will be created.

Edit the Command Code

protected $signature = 'batch:sitemap';

protected $description = 'Regularly create a sitemap';

public function __construct()
{
    parent::__construct();
}

public function handle()
{
        //Destination
        $file = resource_path('views/sitemap.xml');
        //Initialization
        $start_content = '<?xml version="1.0" encoding="UTF-8"?>'
        .'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        \File::put($file,$start_content);

        $content = "<url>
            <loc>".route('home')."</loc>
            <lastmod>2021-06-02</lastmod>
            </url>";
        \File::append($file,$content);

        //Closing tag is necessary
        $end_content = '</urlset>';
        \File::append($file,$end_content);

        //In production mode, ping Google
        if(config('app.env') == 'production'){
            'https://www.google.com/ping?sitemap=https://xxxxxxxxxxx.jp/sitemap.xml';
        }
}

With this, when you run the command, a file with this content will be generated.

resouces/views/sitemap.xml

<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url>
            <loc>http://localhost/home</loc>
            <lastmod>2021-06-02</lastmod>
            </url></urlset>

The meaning of the tags is not explained here.

This time, only a single page for home is being created.

Creating the Response

web.php

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

Create the controller using the command

php artisan make:controller SitemapController

App/Http/Controllers/SitemapController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SitemapController extends Controller
{
    public function index()
    {
        return response(file_get_contents(resource_path('views/sitemap.xml')),200,['Content-Type'=>'application/xml']);
    }
}

You are all set.

Now, try accessing

http://127.0.0.1:8000/sitemap.xml

on your local server.

If it displays like that, then it's fine.

There are some notes written above, but it is okay to submit this to Search Console.

Writing Multiple URLs

You just need to append more entries.

It's not particularly difficult.

SitemapCommand.php

$content = "<url>
    <loc>".route('home')."</loc>
    <lastmod>2021-06-02</lastmod>
    </url>";
\File::append($file,$content)
$content = "<url>
    <loc>".route('post')."</loc>
    <lastmod>2021-06-02</lastmod>
    </url>";
\File::append($file,$content);

That's it.

Conclusion

How was it?

We discussed creating a sitemap in Laravel by brute force.

Honestly, it seems easier than understanding a package, but what do you think...?

If there are only simple URLs like this, using a package might be faster.

However, when it comes to running a service, I think it is easier to customize without packages.

I will refrain from providing specific examples here.

If you find anything odd, feel free to reach out to me via Twitter.

Popular posts

Deploying a PHP7.4 + Laravel6 Project to AWS EC2

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!