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

Learn the API

Mario Bašić edited this page Apr 12, 2015 · 24 revisions

I'd like to show you how to use the API for each class: Sitemap, Sitemap Index, Url and UrlSet.

UrlSet

This is the class that you would want to use if you were creating a sitemap.

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

$sitemap = new Sitemap(new Writer)

addUrl($location, $priority = null, $changeFrequency = null, $lastModification = null, array $translations = [])

This method adds a Url to the sitemap (UrlSet).

  • $location is required and it must be a valid URL.
  • $priority is optional and can be a number >=0 && <= 1.
  • $changeFrequency is optional and can be: "always", "hourly", "daily", "weekly", "monthly", "yearly", "never".
  • $lastModification is optional and you can use Carbon to set the date.
  • $translations is optional and must be a array with keys: hreflang and href. href must be a valid URL.
$sitemap->addUrl('http://acme.me');

// or

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

This method also return the instance of class Url. If you want to know what you can do with that instance see here.

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

generate()

You will probably not use this method, but if you insist you will get the sitemap index XML output in string type.

return $sitemapIndex->generate();

render()

Now this is a method that you will want to use. It returns XML response.

return $sitemapIndex->render();

Url

This class is only used from UrlSet class as a returned object from addUrl method. See above.

SitemapIndex

This is the class that you would want to use if you were creating a sitemap index.

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

$sitemapIndex = new SitemapIndex(new Writer);

addSitemap($location, $lastModification = null)

This will add a sitemap to sitemap index.

  • $location is required and it must be a valid URL.
  • $lastModification is optional and you can use Carbon to set the date.
$sitemapIndex->addSitemap('http://acme.me/sitemap-tags.xml');

// or

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

This method also return the instance of class Sitemap. If you want to know what you can do with that instance see here.

Example:

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

generate()

You will probably not use this method, but if you insist you will get the sitemap index XML output in string type.

return $sitemapIndex->generate();

render()

Now this is a method that you will want to use. It returns XML response.

return $sitemapIndex->render();

Sitemap

This class is only used from SitemapIndex class as a returned object from addSitemap method. See above.

setLocation($url)

This will validate given URL and set it as sitemap location.

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

setLastModification(DateTime $lastModification)

This method sets the last modification date on sitemap.

$tags->setLastModification(Carbon::now());
Clone this wiki locally