Saturday, April 19, 2014

sitemap generator for Laravel 4

Installation


Add the following to your composer.json file :


"roumen/sitemap": "dev-master"

Then register this service provider with Laravel :


'Roumen\Sitemap\SitemapServiceProvider',

Publish configuration file. (OPTIONAL)


php artisan config:publish roumen/sitemap

 


Return dynamic sitemap


Dynamic sitemap means that everytime the URL http://domain.com/sitemap is called we gather all current URL’s and return then in XML or whatever else supported format.


For better optimization and for reducing the stress on the system we can cache that sitemap for a specified time period using this plugin also.


In order to return the sitemap we first have to define a route to handle our request /sitemap:


File: routes.php


Folder: app


You can add this directly to your `routes.php` file or you can place it somewhere else for better separation of code.


Route::get('sitemap', function()

// create new sitemap object
$sitemap = App::make("sitemap");

// set cache (key (string), duration in minutes
// (Carbon);

 


And that’s it! Now you have a sitemap for your application. Don’t forget, you still have to include all of yours URL’s in the above code using the same syntax.


 


 


 


Example: How to create dynamic sitemap


Route::get('sitemap', function()int), turn on/off (boolean))
// by default cache is disabled
$sitemap->setCache('laravel.sitemap', 3600);

// 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
$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');

);

 


Example: How to generate sitemap file


Route::get('mysitemap', function()

// create new sitemap object
$sitemap = App::make("sitemap");

// add items 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
$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);


// generate your sitemap (format, filename)
$sitemap->store('xml', 'mysitemap');
// this will generate file mysitemap.xml to your public folder

);

 


Download  sitemap generator for Laravel 4



sitemap generator for Laravel 4

No comments:

Post a Comment