Last active
August 29, 2015 14:02
-
-
Save damonsharp/526605f1e0c94f912dea to your computer and use it in GitHub Desktop.
Simple Laravel XML Sitemap Generation based on routes file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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