Home php Laravel Generate Sitemap using roumen/sitemap package

Laravel Generate Sitemap using roumen/sitemap package

by Shahriar Sagor

Today now in this post i want to show you example of how to generate dynamic sitemap by using using “roumen/sitemap” package. We are know that sitemap is very useful for SEO. We need to submit our sitemap on google webmaster, bing webmaster etc on that way we can easily increase our website rank and also web traffic. So, if we are working on Laravel framework then we can use “roumen/sitemap”. By using this package we can generate dynamic sitemap for our website.

so it’s a very simple way to use and also pretty interesting package. we can also can add image on sitemap. Also in this package provide us to generate the sitemap and it store it on our folder etc.

So, now let’s need to follow the following step and use it. Here i will provide you just two step one for installing packages and then second one for example of sitemap generate.

Step 1: Installation Package

At first we need to fire the following command on our terminal and we get latest version of “roumen/sitemap” package.

composer require roumen/sitemap

Add ServiceProvider

Ok Good , Now need to open config/app.php file and need to add service provider of this package.

'providers' => [




Publish Config File

Now following bellow command through we can create a publish config file of “roumen/sitemap” package. So after the fire this command we can see one file as app/config/sitemap.php and we can also change our own setting.

php artisan vendor:publish --provider="Roumen\Sitemap\SitemapServiceProvider"
Step 2: Example With Route

Now in the last step i will provide an example of how to generate a sitmap. so at first open app/Http/routes.php and need add following route.


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')
                    ,'post.title as postTitle','post.slug as postSlug'
                    ,'post_image.image as image','post.updated_at as postUpdated_at')

    $postResult = array();
        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');

We can also get more informations of this package from here : Wiki

Read Also : How to convert Object into Array in PHP?

Thanks for read. I hope it help you. For more you can follow us on facebook.


You may also like