Laravel Generate Sitemap using roumen/sitemap package Example

Laravel Generate Sitemap using roumen/sitemap package Example

In this post we will give you information about Laravel Generate Sitemap using roumen/sitemap package Example. Hear we will give you detail about Laravel Generate Sitemap using roumen/sitemap package ExampleAnd how to use it also give you demo for it if it is necessary.

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' => [

....

'RoumenSitemapSitemapServiceProvider',

]

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="RoumenSitemapSitemapServiceProvider"


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

Hope this code and post will helped you for implement Laravel Generate Sitemap using roumen/sitemap package Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US