Skip to content
This repository was archived by the owner on Sep 17, 2018. It is now read-only.

Laravel and Bard

Mario Basic edited this page Apr 1, 2015 · 8 revisions

Dependency injection in Laravel

If you are using Laravel I suggest using dependency injection when dealing with Bard. Because Bard class for creating a sitemap is called UrlSet and not Sitemap I suggest setting an alias for UrlSet to Sitemap:

use Laravelista\Bard\UrlSet as Sitemap;

I think this is more readable because in the future when you see class Sitemap you will know what's it for, but when you see UrlSet you will be like what is this thing here :)

Below you can see an example on how to use DI and create a sitemap by adding URLs.

<?php namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laravelista\Bard\UrlSet as Sitemap;

class SitemapController {

    protected $sitemap;

    public function __construct(Sitemap $sitemap) 
    {
        $this->sitemap = $sitemap;
    }

    public function generate()
    {
        $home = $this->sitemap->addUrl(route('home'));
        $home->setPriority(0.8);
        $home->setChangeFrequency('hourly');
        $home->setLastModification(Carbon::now());
        $home->addTranslation("de", route('home'));

        $this->sitemap->addUrl(
            route('contact'), 
            0.7, 
            'monthly', 
            Carbon::now(), 
            [
                [
                    "hreflang" => 'de', 
                    'href' => route('contact')
                ]
            ]
        );

        $this->sitemap->addUrl(route('gallery'));

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

}

Resolve out of Service Container IoC in Laravel

$sitemap = App::make('Laravelista\Bard\UrlSet')

The preferred way of using Bard in Laravel is with Dependency Injection.


If you are using Laravel or any other php framework be sure to check out Laravel Boilerplate for out-of-the-box methods when dealing with common tasks like adding named routes with or without translations.

Clone this wiki locally