Skip to content
This repository was archived by the owner on Sep 17, 2018. It is now read-only.
Mario Basic edited this page Apr 1, 2015 · 12 revisions

There are 2 main classes in Bard.

If you have ever seen how a sitemap in xml looks like you will know how to use those classes just by looking at their names, but if it still not clear let me explain.

  1. UrlSet - If you want to create a sitemap (Sitemap is a set of URLs) use this class
  2. SitemapIndex - If you want to create a sitemap index (Sitemap index is a set of sitemaps) use this class

Framework Agnostic Approach

Creating a Sitemap

UrlSet has a method called addUrl and with that method you can add URLs to the set of URLs in the UrlSet class. Each URL added is a instance of class Url and it has its own methods and properties with which you can interact.

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

$sitemap = new Sitemap(new Writer);

$home = $sitemap->addUrl('http://acme.me');
$home->setPriority(0.8);
$home->setChangeFrequency('hourly');
$home->setLastModification(Carbon::now());
$home->addTranslation("de", 'http://acme.me/de/contact');*/

$sitemap->addUrl(
    'http://acme.me/contact', 
    0.5, 
    null, 
    new DateTime('2015-04-01'), 
    [
        [
            "hreflang" => 'de', 
            'href' => 'http://acme.me/de/contact'
        ]
    ]
);

$sitemap->addUrl('http://acme.me/gallery');

return $sitemap->render();

Creating a Sitemap Index

SitemapIndex has a method addSitemap and with that method you can add Sitemaps to the set of Sitemaps in the SitemapIndex class. Each Sitemap added is a instance of class Sitemap and it has its own methods and properties with which you can interact.

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

$sitemapIndex = new SitemapIndex(new Writer);

$sitemapIndex->addSitemap('http://acme.me/sitemap-tags.xml');

$events = $sitemapIndex->addSitemap('http://acme.me/sitemap-events.xml');
$events->setLocation('http://acme.me/sitemap-events2.xml');
$events->setLastModification(Carbon::now());

$sitemapIndex->addSitemap(
    'http://acme.me/sitemap-people.xml', 
    Carbon::now()
);

$sitemapIndex->addSitemap(
    'http://acme.me/sitemap-comments.xml', 
    new DateTime('2015-04-01')
);


return $sitemapIndex->render();
Clone this wiki locally