Creating a Sitemap with Bard 2.0

This version of Bard brings simpler (new) syntax, easier sitemap creation, and it will completely break your application if you have used the older version of Bard before :)



Introduction

A few days ago, I published a website Visit Murter where you can explore and see the destinations on the island Murter. It is a relatively big website and I needed to create a sitemap for it. I already had a package for sitemap creation called Bard, but at that moment the last commit to Bard was made in March 2015.

The package works and I have used it on many sites that I have created before. Even this site (Laravelista) uses Bard for Sitemap creation, but after working with it many times I have come to a conclusion that I can make it easier to get started with Bard.

I must admit, that every time that I did composer require laravelista/bard my next step was going to the documentation and figuring out how to implement the "Laravel boilerplate" that came with Bard. The boilerplate enabled you to easily implement translations and use route names instead of URLs. It was something that I was proud when I first created it, but soon I realized that it was a big pain in the ass.

This new version of Bard almost completely breaks the old way of doing things. Before you could have used the constructor to create the URL or you could have set only the location and then use multiple method calls to set the desired properties on the URL. In this version, there is only one way of doing things and that is by chaining methods. Also, setting translations has also been simplified.

In this tutorial, I will show you how to write a simple PHP file that generates an example sitemap using Bard, and in the next tutorial, I will show you how to use Bard and Laravel on the same example.

New project

In the place where you keep your repositories aka projects, create a new folder and call it bard-php. Inside it run composer init. Follow the setup process and at the end confirm generation.

This is what I got:

{
    "name": "laravelista/bard-php",
    "description": "Example PHP script that demonstrates how to create a sitemap with Bard.",
    "license": "MIT",
    "authors": [
        {
            "name": "Mario Bašić",
            "email": "mario.basic@outlook.com"
        }
    ],
    "require": {}
}

Now to create a license file. Create a file called license.md and inside it paste the following:

The MIT License (MIT)

Copyright (c) 2017 Mario Bašić

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Replace the Author name and year with the one that suits you.

I will create a readme file for this project readme.md. It will be pushed to GitHub and that is why we are creating the license and readme files.

This is the current state of the repository at this moment b10660acf7982d3a2c1d39e0d630f15e46069949.

Installation

Now to install Bard we need to run:

composer require laravelista/bard

Output:

$ composer require laravelista/bard
Using version ^2.0 for laravelista/bard
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/polyfill-mbstring (v1.4.0)
    Downloading: 100%

  - Installing symfony/translation (v3.3.3)
    Downloading: 100%

  - Installing nesbot/carbon (1.22.1)
    Loading from cache

  - Installing symfony/http-foundation (v3.3.3)
    Downloading: 100%

  - Installing sabre/uri (1.2.1)
    Loading from cache

  - Installing sabre/xml (0.4.3)
    Loading from cache

  - Installing laravelista/bard (2.0.0)
    Loading from cache

This means that Bard is installed, but there are a few things that we need to do now. We need to tell Git to ignore the vendor folder. We do that by creating a .gitignore file in the root of our project and inside it write vendor/ like so:

.gitignore

vendor/

Awesome!

This is the current state of the repository at this moment 77b94ee58ff68e9c3564ad4f9c621430187f46fb.

Creating a sitemap

Create a file called sitemap.php and inside it place the following code:

<?php

require('vendor/autoload.php');

echo 'hello';

Now run php -S localhost:8000 to test that everything is working. Point your browser to http://localhost:8000/sitemap.php. You should see hello text displayed.

Great!

Now let's replace the line echo 'hello'; with the following:

use Laravelista\Bard\UrlSet;
use Sabre\Xml\Writer;
use Carbon\Carbon;

$sitemap = new UrlSet(new Writer);

$sitemap->addUrl('http://domain.com')
    ->setPriority(1.0)
    ->setChangeFrequency('always')
    ->addTranslation('hr', 'http://domain.com/hr');

$sitemap->addUrl('http://domain.com/contact')
    ->setPriority(0.8)
    ->setChangeFrequency('hourly')
    ->setLastModification(Carbon::now())
    ->addTranslation('hr', 'http://domain.com/hr/contact');

$sitemap->render()->send();

Save the changes. If you check your browser now, you will see:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <url>
        <loc>http://domain.com</loc>
        <priority>1.0</priority>
        <changefreq>always</changefreq>
        <xhtml:link rel="alternate" hreflang="hr" href="http://domain.com/hr"/>
    </url>
    <url>
        <loc>http://domain.com/contact</loc>
        <priority>0.8</priority>
        <changefreq>hourly</changefreq>
        <lastmod>2017-07-05T10:48:57+00:00</lastmod>
        <xhtml:link rel="alternate" hreflang="hr" href="http://domain.com/hr/contact"/>
    </url>
</urlset>

This is the current state of the repository at this moment 9c4acda49a9fa98354aa10f1d770e1dc4e790080.

Simplest example ever

The simplest example of creating a sitemap would be this:

<?php

require('vendor/autoload.php');

use Laravelista\Bard\UrlSet;
use Sabre\Xml\Writer;

$sitemap = new UrlSet(new Writer);

$sitemap->addUrl('http://domain.com');
$sitemap->addUrl('http://domain.com/contact');

$sitemap->render()->send();

and it would return:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <url>
        <loc>http://domain.com</loc>
    </url>
    <url>
        <loc>http://domain.com/contact</loc>
    </url>
</urlset>

Thank you for reading. Stay tuned for the next tutorial on creating a sitemap with Bard and Laravel.

P.S. Give Bard a star on GitHub.

Credits

Comments