|
|
#### Example: How to create dynamic sitemap
|
|
|
|
|
|
```php
|
|
|
Route::get('sitemap', function(){
|
|
|
|
|
|
// create new sitemap object
|
|
|
$sitemap = App::make("sitemap");
|
|
|
|
|
|
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
|
|
|
// by default cache is disabled
|
|
|
$sitemap->setCache('laravel.sitemap', 60);
|
|
|
|
|
|
// check if there is cached sitemap and build new only if is not
|
|
|
if (!$sitemap->isCached())
|
|
|
{
|
|
|
// add item to the sitemap (url, date, priority, freq)
|
|
|
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
|
|
|
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
|
|
|
|
|
|
// add item with translations (url, date, priority, freq, images, title, translations)
|
|
|
$translations = [
|
|
|
['language' => 'fr', 'url' => URL::to('pageFr')],
|
|
|
['language' => 'de', 'url' => URL::to('pageDe')],
|
|
|
['language' => 'bg', 'url' => URL::to('pageBg')],
|
|
|
];
|
|
|
$sitemap->add(URL::to('pageEn'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', [], null, $translations);
|
|
|
|
|
|
// add item with images
|
|
|
$images = [
|
|
|
['url' => URL::to('images/pic1.jpg'), 'title' => 'Image title', 'caption' => 'Image caption', 'geo_location' => 'Plovdiv, Bulgaria'],
|
|
|
['url' => URL::to('images/pic2.jpg'), 'title' => 'Image title2', 'caption' => 'Image caption2'],
|
|
|
['url' => URL::to('images/pic3.jpg'), 'title' => 'Image title3'],
|
|
|
];
|
|
|
$sitemap->add(URL::to('post-with-images'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', $images);
|
|
|
|
|
|
// get all posts from db
|
|
|
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
|
|
|
|
|
|
// add every post to the sitemap
|
|
|
foreach ($posts as $post)
|
|
|
{
|
|
|
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
|
|
|
return $sitemap->render('xml');
|
|
|
|
|
|
Route::get('sitemap', function() {
|
|
|
|
|
|
// create new sitemap object
|
|
|
$sitemap = App::make('sitemap');
|
|
|
|
|
|
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
|
|
|
// by default cache is disabled
|
|
|
$sitemap->setCache('laravel.sitemap', 60);
|
|
|
|
|
|
// check if there is cached sitemap and build new only if is not
|
|
|
if (!$sitemap->isCached()) {
|
|
|
// add item to the sitemap (url, date, priority, freq)
|
|
|
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
|
|
|
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
|
|
|
|
|
|
// add item with translations (url, date, priority, freq, images, title, translations)
|
|
|
$translations = [
|
|
|
['language' => 'fr', 'url' => URL::to('pageFr')],
|
|
|
['language' => 'de', 'url' => URL::to('pageDe')],
|
|
|
['language' => 'bg', 'url' => URL::to('pageBg')],
|
|
|
];
|
|
|
$sitemap->add(URL::to('pageEn'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', [], null, $translations);
|
|
|
|
|
|
// add item with images
|
|
|
$images = [
|
|
|
['url' => URL::to('images/pic1.jpg'), 'title' => 'Image title', 'caption' => 'Image caption', 'geo_location' => 'Plovdiv, Bulgaria'],
|
|
|
['url' => URL::to('images/pic2.jpg'), 'title' => 'Image title2', 'caption' => 'Image caption2'],
|
|
|
['url' => URL::to('images/pic3.jpg'), 'title' => 'Image title3'],
|
|
|
];
|
|
|
$sitemap->add(URL::to('post-with-images'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', $images);
|
|
|
|
|
|
// get all posts from db
|
|
|
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
|
|
|
|
|
|
// add every post to the sitemap
|
|
|
foreach ($posts as $post) {
|
|
|
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
|
|
|
return $sitemap->render('xml');
|
|
|
});
|
|
|
```
|
|
|
|
... | ... | @@ -54,44 +51,41 @@ Route::get('sitemap', function(){ |
|
|
In this example the posts model has a `belongsToMany` relationship to images.
|
|
|
|
|
|
```php
|
|
|
Route::get('sitemap', function(){
|
|
|
|
|
|
// create new sitemap object
|
|
|
$sitemap = App::make("sitemap");
|
|
|
|
|
|
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
|
|
|
// by default cache is disabled
|
|
|
$sitemap->setCache('laravel.sitemap', 60);
|
|
|
|
|
|
// check if there is cached sitemap and build new only if is not
|
|
|
if (!$sitemap->isCached())
|
|
|
{
|
|
|
// add item to the sitemap (url, date, priority, freq)
|
|
|
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
|
|
|
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
|
|
|
|
|
|
// get all posts from db, with image relations
|
|
|
$posts = \DB::table('posts')->with('images')->orderBy('created_at', 'desc')->get();
|
|
|
|
|
|
// add every post to the sitemap
|
|
|
foreach ($posts as $post)
|
|
|
{
|
|
|
// get all images for the current post
|
|
|
$images = array();
|
|
|
foreach ($post->images as $image) {
|
|
|
$images[] = array(
|
|
|
'url' => $image->url,
|
|
|
'title' => $image->title,
|
|
|
'caption' => $image->caption
|
|
|
);
|
|
|
}
|
|
|
|
|
|
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq, $images);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
|
|
|
return $sitemap->render('xml');
|
|
|
|
|
|
Route::get('sitemap', function() {
|
|
|
|
|
|
// create new sitemap object
|
|
|
$sitemap = App::make('sitemap');
|
|
|
|
|
|
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
|
|
|
// by default cache is disabled
|
|
|
$sitemap->setCache('laravel.sitemap', 60);
|
|
|
|
|
|
// check if there is cached sitemap and build new only if is not
|
|
|
if (!$sitemap->isCached()) {
|
|
|
// add item to the sitemap (url, date, priority, freq)
|
|
|
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
|
|
|
$sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
|
|
|
|
|
|
// get all posts from db, with image relations
|
|
|
$posts = \DB::table('posts')->with('images')->orderBy('created_at', 'desc')->get();
|
|
|
|
|
|
// add every post to the sitemap
|
|
|
foreach ($posts as $post) {
|
|
|
// get all images for the current post
|
|
|
$images = array();
|
|
|
foreach ($post->images as $image) {
|
|
|
$images[] = array(
|
|
|
'url' => $image->url,
|
|
|
'title' => $image->title,
|
|
|
'caption' => $image->caption
|
|
|
);
|
|
|
}
|
|
|
|
|
|
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq, $images);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
|
|
|
return $sitemap->render('xml');
|
|
|
});
|
|
|
``` |
|
|
\ No newline at end of file |