Bard 2.0 and Laravel

Using Bard with Laravel is much simpler than before. Let me show you.



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 create an example sitemap using Bard and Laravel.

New project

From the place where you keep your repositories aka projects run this command to create a new Laravel application:

laravel new bard-laravel

If you don't have the laravel command installed on your PC, see the documentation on Installing Laravel Via Laravel Installer.

This will create a fresh Laravel installation with all of Laravel's dependencies already installed in the directory bard-laravel. When the installation completes you will see this message at the end:

Application ready! Build something amazing.

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

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 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. Awesome!

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

Creating a sitemap

In routes/web.php add this code:

Route::get('sitemap.xml', function(\Laravelista\Bard\UrlSet $sitemap) {
    return 'Hello';
});

Now run php artisan serve to test that everything is working. Point your browser to http://localhost:8000/sitemap.xml. You should see Hello text displayed.

Great!

Now let's replace the line return 'Hello'; with the following:

$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');

return $sitemap->render();

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-07T10:53:03+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 30738c9bc56e9eed702750258b0982bb0615f920.

Simplest example ever

The simplest example of creating a sitemap would be this:

Route::get('sitemap.xml', function(\Laravelista\Bard\UrlSet $sitemap) {
    $sitemap->addUrl('http://domain.com');
    $sitemap->addUrl('http://domain.com/contact');

    return $sitemap->render();
});

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.

P.S. Give Bard a star on GitHub.

Credits

Comments