Skip Navigation

Configure Laravel Sitemap

In the second video of our "Laravel Sitemap" series, "Configure," CodeTime instructor Trevor Greenleaf shows you how to install the Laravel Sitemap package and configure it.


So time to configure Laravel sitemap. This is actually pretty easy to get started with. If you scroll down to the bottom here, it says composer require, I guess not bottom but middle here, top middle. Composer require Spatie/Laravel-sitemap. That's the line that you'll need to go and run. Now, of course, I'm assuming you have a Laravel install already and you have routes already set up and you have some links already set up. Otherwise, not sure what you're doing here, right? I have a basic demo site configured. It's a brand new Laravel install. I even made a couple of routes. So here's '/,' 'demo,' 'cats,' 'cats/1,' 'cats/baba,' 'home.' I also have the Auth::routes(). I ran the artist and Auth make routes commands. You'll see the Auth routes and then the route/home, Controller, index name, home.

Okay. So that's all our routes. And if you go to my welcome.blade, notice these all go to the same page, all the same view, just the view for welcome. In the welcome page, I added links to those pages. So again, you'll see links to the log-in and registration, but home won't work because it's on Auth, right? Auth or else. Okay. So that's what we're expecting. We're expecting these links right here to show up in that Saya map when we start to integrate it. I've done that, I've set that up, and then in my env file, I'm referencing a database called Laravel backup demo. And I just called it that as, I'm also using this Laravel site for the Laravel backup demo here on code time as well. So if you want to back up your Laravel install, definitely check out that series as well on backing up your Laravel install.

Anyways, that's my basic configuration. Here's my welcome, here's my web, that's all I've done. Time to go in and install this package. So I'm going to use composer requires Spatie Laravel sitemap, and I'm going to type it up into the top, into my terminal. And I'm just using iTerm2. And if you like the sort of configuration that I have, and you're interested about setting up yours that way, you can check out Code Time video for iTerm2 and it'll show you how to go and configure that and set it up to have the colors and any sort of symbols or icons that I'm also using with, iTerm2 in my command line. But of course this will work with Terminal or whatever else you're using, as it's a pretty basic composer require. Okay. So while that's running, if we scroll down a little bit, this is the configuration options for it, and this is pretty common. Most packages, well a lot of packages come with a configuration that you can modify the way that they actually work.

Well, let's copy that line and paste that in as well. So mine's finished installing and publish is complete. So now if I go and look at my composer.json file, I see a Spatie Laravel sitemap and that's all good. And then if I go to my config file, I see the sitemap file. And this is the sitemap file that we're talking about the configuration for. Okay. So that's the base, we're set up there. We're actually ready to now go and run our first test, our first generation of a sitemap. To do that, this is just the configuration. If we scroll down a little bit, it says, generate a sitemap. Well, this is it. This is the only line you need to really run to make a sitemap. Of course, we're going to take it a little bit further than that, but let's go and grab that line.

Then under sitemap here, I'm going to close this and as I don't need to do any configuration to that as of this point. I'm in my web routes file, and eventually I want to get to making this into a console command, but for now I just want to test it. So I'm going to make a route called sitemap. And the goal there is, when I hit that sitemap route, it's going to go and index all the pages that I've linked to and then produce a sitemap.xml inside the public directory. So let's give that a try. So I'm going to say a route and ::get, and then what do I want to get? I want to get sitemap, and then we'll run this as our little function and then put a semi-colon and then we'll say return, let's say sitemap created. Then here, I want to actually go and run that line.

It's going to say SitemapGenerator, create https For this, I want to write in the name of the site that I wanted to query, which is the site that I have set up here. And I'm using Laravel Valet. You'll notice the test http://laravel.test. But if you're running a map or something else, of course you'll know what your path is for that, and you could put that in there. What's cool about this is I'm not sure why you'd want to do that, but you could put any URL in here and generate a sitemap from it. So if you wanted to make a little quick sitemap generator tool for people maybe who aren't comfortable doing all this work, and it just automatically generates a sitemap, you could easily make a tool for them to do that.

Now the path here, this is going to be the path where it's going to go and save it to, by default is going to save to public directory. So if you wanted to change that up, you could, but I'm going to set this up to go to sitemap.XML, which is the default file name for a sitemap. And it's going to be in the public directory when it saves. So this is all good, but in order to use sitemap generator, I need to go up top and say, use sitemap generator, and if you scroll back up to the top here, it says, use sitemaps, SitemapGenerator, or use Spatie\Sitemap\SitemapGenerator. All right. So this is set up and again, I'm just doing this for demonstration purposes. I'll be moving this over into a console command in the next episode to show you how that process works and configuration.

Okay. I think we're good. Let's give it a try. So we're expecting, when we go to sitemap, it's going to hit this URL. It's going to index any pages, any links that are on that page, it's going to add those to the sitemap.xml file. It's going to return back 'sitemap created' just as plain text. And then when we come back and look inside of our public directory, we should see the sitemap.xml. All right, here we go, let's run it. So I'm going to type in sitemap. Yep.

And it said sitemap created. Okay, that sounds good, but let's see if it actually happened. Here's our sitemap.xml and here are all our pages. So Laravel tests about-us, cats, cats/one, Baba, demo, login, password reset, register. All right. There's all of our pages, look how fast that was. It just indexed it. We have those pages. It's in the XML file now. And you could say we're done, right? And anytime you wanted to go and update your sitemap, you just hit sitemap and it'll update your sitemap.

Well, I guess that would work and it's a pretty simple solution, but I like to do it so I don't have to hit that URL once a week, right? If I'm uploading tons of posts or pages or whatever my model is, I don't want to have to remember to hit that URL. I just want it to go and do its thing. Go and generate me a sitemap, give it a task and it'll go do it by itself. You don't need to worry about it anymore. Well, in order to do that, we need to set up a console command and we need to set it up with the kernel. So I'm going to show you how to do that in the next episode. Let's go ahead and jump to that.