Skip to content

Instantly share code, notes, and snippets.

@damonsharp
Last active August 29, 2015 14:02
Show Gist options
  • Save damonsharp/526605f1e0c94f912dea to your computer and use it in GitHub Desktop.
Save damonsharp/526605f1e0c94f912dea to your computer and use it in GitHub Desktop.
Simple Laravel XML Sitemap Generation based on routes file
// XML sitemap - requires this package: https://github.com/RoumenDamianoff/laravel-sitemap
// NOTE: This example uses a closure within routes.php. If attempting this in a controller,
// especially with Laravel 5+, make sure to include the required classes, which in this case
// would be...
// use App;
// use URL;
// if utilizing the Carbon library for dates, also include Carbon...
// use Carbon\Carbon;
Route::get('/xml-sitemap', array('as' => 'xml-sitemap', function(){
$sitemap = App::make("sitemap");
$dt = new DateTime();
$dt = $dt->format(DateTime::ISO8601);
$routes = Route::getRoutes();
foreach ( $routes as $route )
{
$uri = $route->getURI();
$path = $route->getPath();
if ( ! preg_match('/xml-sitemap/', $path) )
{
$priority = ( preg_match('/^\//', $uri ) ) ? '1.0' : '0.9';
$sitemap->add(URL::to($path), $dt, $priority, 'daily');
}
}
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
return $sitemap->render('xml');
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment