-
Notifications
You must be signed in to change notification settings - Fork 3
Learn the API
I'd like to show you how to use the API for each class: UrlSet
, Url
, SitemapIndex
and Sitemap
.
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; // Optional
$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 an array of arrays with keys:hreflang
andhref
.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 Url
.
$home = $sitemap->addUrl('http://acme.me');
You will probably not use this method, but if you insist you will get the sitemap XML output in string type.
return $sitemap->generate();
Now this is a method that you will want to use. It returns output from generate()
method as XML response.
return $sitemap->render();
This class is only used from UrlSet
class as a returned object from addUrl
method. See addUrl
.
-
$url
must be a valid URL.
This will validate given URL and set it as Url location.
$home->setLocation('http://acme.me/dashboard');
-
$lastModification
must beDateTime
. You can use Carbon to set the date.
This method sets the last modification date on Url.
$home->setLastModification(Carbon::now());
This sets translations for Url.
-
$translations
must be a array of arrays with keys:hreflang
andhref
.href
must be a valid URL.
$translations = [
[
"hreflang" => 'de',
'href' => 'http://acme.me/de'
],
[
"hreflang" => 'hr',
'href' => 'http://acme.me/hr'
]
]
$home->setTranslations($translations);
This adds a single translation to Url
.
There are two ways of using this method.
- Pass arguments:
hreflang
andhref
$home->addTranslation('hr', 'http://acme.me/hr');
- Pass an array containing:
hreflang
andhref
keys
$translation = [
"hreflang" => 'hr',
'href' => 'http://acme.me/hr'
]
$home->addTranslation($translation);
href
must be valid URL.
This sets priority for Url
.
-
$priority
must be a number between>=0 && <= 1
$home->setPriority(0.8)
This sets the change frequency for Url
.
-
$changeFrequency
can be:"always", "hourly", "daily", "weekly", "monthly", "yearly", "never"
.
$home->setChangeFrequency('hourly');
This returns an array of valid change frequencies for Url
.
Important: This is only used for testing with PHPSpec.
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);
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 Sitemap
.
$tags= $sitemapIndex->addSitemap('http://acme.me/sitemap-tags.xml');
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();
Now this is a method that you will want to use. It returns output from generate()
method as XML response.
return $sitemapIndex->render();
This class is only used from SitemapIndex
class as a returned object from addSitemap
method. See addSitemap
.
This will validate given URL and set it as sitemap location.
$tags->setLocation('http://acme.me/sitemap-tags-index.xml');
This method sets the last modification date on sitemap.
$tags->setLastModification(Carbon::now());
- The cool image with Bard flying and Bard logo are from Bard, the Wandering Caretaker revealed website.