2017 © Pedro Peláez
 

library sitemap

Generate Google Sitemaps in Laravel 4/5

image

watson/sitemap

Generate Google Sitemaps in Laravel 4/5

  • Friday, July 13, 2018
  • by dwightwatson
  • Repository
  • 11 Watchers
  • 180 Stars
  • 207,600 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 35 Forks
  • 4 Open issues
  • 34 Versions
  • 11 % Grown

The README.md

Sitemap for Laravel

Build Status Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

Sitemap is a package built specifically for Laravel that will help you generate XML sitemaps for Google. Based on laravel-sitemap this package operates in a slightly different way to better fit the needs of our project. A facade is used to access the sitemap class and we have added the ability to produce sitemap indexes as well as sitemaps. Furthermore, it's tested., (*2)

Read more about sitemaps and how to use them efficiently on Google Webmaster Tools., (*3)

Installation

composer require watson/sitemap

Usage

Creating sitemap indexes

If you have a large number of links (50,000+) you will want to break your sitemaps out into seperate sitemaps with a sitemap index linking them all. You add sitemap indexes using Sitemap::addSitemap($location, $lastModified) and then you return the sitemap index with Sitemap::renderSitemapIndex(). The $lastModified variable will be parsed and converted to the right format for a sitemap., (*4)

Here is an example controller that produces a sitemap index., (*5)

namespace App\Http\Controllers;

use Sitemap;

class SitemapsController extends Controller
{
    public function index()
    {
        // Get a general sitemap.
        Sitemap::addSitemap('/sitemaps/general');

        // You can use the route helpers too.
        Sitemap::addSitemap(route('sitemaps.posts'));

        // Return the sitemap to the client.
        return Sitemap::index();
    }
}

Simply route to this as you usually would, Route::get('sitemap', 'SitemapsController@index');., (*6)

Creating sitemaps

Similarly to sitemap indexes, you just add tags for each item in your sitemap using Sitemap::addTag($location, $lastModified, $changeFrequency, $priority). You can return the sitemap with Sitemap::renderSitemap(). Again, the $lastModified variable will be parsed and convered to the right format for the sitemap., (*7)

If you'd like to just get the raw XML, simply call Sitemap::xml()., (*8)

Here is an example controller that produces a sitemap for blog posts., (*9)

namespace App\Http\Controllers;

use Post;
use Sitemap;

class SitemapsController extends Controller
{
    public function posts()
    {
        $posts = Post::all();

        foreach ($posts as $post) {
            Sitemap::addTag(route('posts.show', $post), $post->updated_at, 'daily', '0.8');
        }

        return Sitemap::render();
    }
}

If you just want to pass a model's updated_at timestamp as the last modified parameter, you can simply pass the model as the second parameter and the sitemap will use that attribute automatically., (*10)

If you're pulling a lot of records from your database you might want to consider restricting the amount of data you're getting by using the select() method. You can also use the chunk() method to only load a certain number of records at any one time as to not run out of memory., (*11)

Image sitemaps

You can use Google image extensions for sitemaps to give Google more information about the images available on your pages. Read the specification, (*12)

Images are associated with page and you can use up to 1000 images per page., (*13)

Here is an example of adding image tag to usual page tag., (*14)

namespace App\Http\Controllers;

use Page;
use Sitemap;

class SitemapsController extends Controller
{
    public function pages()
    {
        $pages = Page::all();

        foreach ($pages as $page) {
            $tag = Sitemap::addTag(route('pages.show', $page), $page->updated_at, 'daily', '0.8');

            foreach ($page->images as $image) {
                $tag->addImage($image->url, $image->caption);
            }
        }

        return Sitemap::render();
    }
}

Here is the full list of arguments to add an image to a tag., (*15)

$tag->addImage($location, $caption, $geoLocation, $title, $licenceUrl);

Configuration

To publish the configuration file for the sitemap package, simply run this Artisan command:, (*16)

php artisan config:publish watson/sitemap

php artisan vendor:publish --provider="Watson\Sitemap\SitemapServiceProvider"

Then take a look in config/sitemap.php to see what is available., (*17)

Caching

By default, caching is disabled. If you would like to enable caching, simply set cache_enabled in the configuration file to true. You can then specify how long you would like your views to be cached for. Keep in mind that when enabled, caching will affect each and every request made to the sitemap package., (*18)

Multilingual tags

If you'd like to use a mutlilingual tag, simply pass an instance of one to the addTag method. Below is a crude example of how you would pass alternate tag locations for different languages., (*19)

Sitemap::addTag(new \Watson\Sitemap\Tags\MultilingualTag(
    $location,
    $lastModified,
    $changeFrequency,
    $priority,
    [
        'en' => $location . '?lang=en',
        'fr' => $location . '?lang=fr'
    ]
));

The Versions

13/07 2018

dev-master

9999999-dev

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

13/07 2018

2.0.22

2.0.22.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

24/04 2018

2.0.21

2.0.21.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

15/01 2018

3.0.x-dev

3.0.9999999.9999999-dev

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

30/06 2017

2.0.20

2.0.20.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

24/12 2016

2.0.19

2.0.19.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

21/11 2016

2.0.18

2.0.18.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

21/10 2016

1.1.x-dev

1.1.9999999.9999999-dev

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

21/10 2016

1.1.5

1.1.5.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

21/10 2016

2.0.17

2.0.17.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

13/10 2016

2.0.16

2.0.16.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

07/09 2016

2.0.15

2.0.15.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

19/08 2016

2.0.14

2.0.14.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

29/07 2016

dev-develop

dev-develop

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

15/05 2016

1.1.4

1.1.4.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

05/04 2016

2.0.13

2.0.13.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

17/02 2016

2.0.12

2.0.12.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

07/02 2016

2.0.11

2.0.11.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

24/01 2016

2.0.10

2.0.10.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

22/11 2015

2.0.9

2.0.9.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

30/09 2015

2.0.8

2.0.8.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

30/09 2015

2.0.7

2.0.7.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

03/07 2015

2.0.6

2.0.6.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

29/05 2015

2.0.5

2.0.5.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

25/02 2015

2.0.4

2.0.4.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

22/02 2015

2.0.3

2.0.3.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

16/12 2014

1.1.3

1.1.3.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

16/12 2014

2.0.2

2.0.2.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

16/10 2014

2.0.1

2.0.1.0

Generate Google Sitemaps in Laravel 4/5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

16/10 2014

2.0.0

2.0.0.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

03/08 2014

1.1.2

1.1.2.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

22/05 2014

1.1.1

1.1.1.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

22/05 2014

1.1.0

1.1.0.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps

07/03 2014

1.0.0

1.0.0.0

Generate Google Sitemaps in Laravel 4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel sitemaps