3 Best Laravel Sitemap Generator Libraries

Sitemap in a Laravel application is very useful for SEO. An app’s owner can submit his app’s sitemap to Google webmaster, and Bing webmaster to enhance SEO.

Fortunately, there are many good packages to generate a sitemap for Laravel.

Laravelium/laravel-sitemap

This sitemap supports up to Laravel 8. So it is good to use in your Laravel application.

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

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

	// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
	// by default cache is disabled
	$sitemap->setCache('laravel.sitemap', 60);

	// check if there is cached sitemap and build new only if is not
	if (!$sitemap->isCached()) {
		// 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');

		// add item with translations (url, date, priority, freq, images, title, translations)
		$translations = [
			['language' => 'fr', 'url' => URL::to('pageFr')],
			['language' => 'de', 'url' => URL::to('pageDe')],
			['language' => 'bg', 'url' => URL::to('pageBg')],
		];
		$sitemap->add(URL::to('pageEn'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', [], null, $translations);

		// add item with images
		$images = [
			['url' => URL::to('images/image1.jpg'), 'title' => 'Image title', 'caption' => 'Image caption', 'geo_location' => 'Plovdiv, Bulgaria'],
			['url' => URL::to('images/image2.jpg'), 'title' => 'Image title2', 'caption' => 'Image caption2'],
			['url' => URL::to('images/image3.jpg'), 'title' => 'Image title3'],
		];
		$sitemap->add(URL::to('post-with-images'), '2015-06-24T14:30:00+02:00', '0.9', 'monthly', $images);

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

spatie/laravel-sitemap

spatie’s laravel-sitemap package can generate a sitemap without you having to add URLs to it manually. This works by crawling your entire site.

//create a sitemap file and add more links to it
SitemapGenerator::create('https://example.com')
   ->getSitemap()
   ->add(Url::create('/additional-page')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))
   ->add(Url::create('/additional-page-2')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.5))
    ->writeToFile($path);

ARCANEDEV/LaravelSitemap

This package allows you to manage and generate a sitemap without you having to add urls to it manually.

use Arcanedev\LaravelSitemap\Entities\Url;

// Example #1
$url = new Url([
    'loc'        => 'http://example.com',
    'lastmod'    => date('Y-m-d H:i:s'), // You can also pass a DateTime object.
    'changefreq' => 'daily',
    'priority'   => 1.0,
    'title'      => 'Homepage - Example',
]);

// Example #2
$url = Url::makeFromArray([
    'loc'        => 'http://example.com',
    'lastmod'    => date('Y-m-d H:i:s'), // You can also pass a DateTime object.
    'changefreq' => 'daily',
    'priority'   => 1.0,
    'title'      => 'Homepage - Example',
]);

// Example #3

$url = Url::make('http://example.com')
          ->setLastMod(date('Y-m-d H:i:s'))
          ->setChangeFreq('daily')
          ->setPriority(1.0)
          ->setTitle('Homepage - Example');

Leave a Comment

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


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close