In this post i want to githubve example of generate dynamic sitemap using “roumen/sitemap” package. sitemap is very usefull for SEO. you can submit your sitemap on google webmaster, bing webmaster etc that way you can increase website rank and web traffic. So, if you are working on Laravel framework then you can use “roumen/sitemap”. this package will helps to generate dynamic sitemap of website. so it’s very simple way to use and pretty interesting package. you can also add image on sitemap. this package is also provide to generate sitemap and store it on you folder etc. you can also see the preview of sitemap.
Preview:
So, let’s follow following step and use it. i provide just two step one for installing packages and second one for example of sitemap generate.
Step 1: Installation Package
first you have to fire following command on your terminal and get latest version of “roumen/sitemap” package.
composer require roumen/sitemap
Add ServiceProvider
Ok, Now open config/app.php file and add service provider of this package.
'providers' => [
....
'Roumen\Sitemap\SitemapServiceProvider',
]
Publish Config File
following command through you can create publish config file of “roumen/sitemap” package. after fire this command you can see one file app/config/sitemap.php and you can change your own setting.
php artisan vendor:publish --provider="Roumen\Sitemap\SitemapServiceProvider"
Step 2: Example With Route
In Last step i provde example of how to generate sitmap. so first open app/Http/routes.php and add following route.
app/Http/routes.php
Route::get('sitemap', function(){
/* create new sitemap object */
$sitemap = App::make("sitemap");
/* add item to the sitemap (url, date, priority, freq) */
$sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$posts = DB::table('post')
->select('post.*'
,'post.title as postTitle','post.slug as postSlug'
,'post_image.image as image','post.updated_at as postUpdated_at')
->join('post_image','post_image.post_id','=','post.id')
->orderBy('created_at','desc')
->groupBy('post.id')
->get();
$postResult = array();
if(!empty($posts)){
foreach ($posts as $key => $value) {
$postResult[$value->id]['id'] = $value->id;
$postResult[$value->id]['slug'] = $value->postSlug;
$postResult[$value->id]['postTitle'] = $value->postTitle;
$postResult[$value->id]['postUpdated_at'] = $value->postUpdated_at;
$postResult[$value->id]['image'][] = $value->image;
}
}
/* add every post to the sitemap */
foreach ($postResult as $key=>$value)
{
$images = array();
foreach ($value['image'] as $key2 => $value2) {
$images[] = array(
'url' => URL::to('/')."/uploadImages/post/".$value2,
'title' => $value['slug'],
'caption' => $value['postTitle']
);
}
$sitemap->add(URL::route('front.post',$value['slug']), $value['postUpdated_at'], '1.0', 'daily', $images);
}
/* show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf') */
return $sitemap->render('xml');
});
Now you can open sitemap route and check output, it will be like preview. You can also get more information of this package from here : Wiki