Skip Navigation

Introduction Laravel Sitemap

In the first video of our "Laravel Sitemap" series, "Introduction," CodeTime instructor Trevor Greenleaf explains what a sitemap is and why it might be useful to create one for your Laravel app.

Transcript

Hello, and welcome to Code Time. My name's Trevor Greenleaf. Today, I'm going to talk about Laravel Sitemap. Laravel Sitemap's a package by Spatie Spatie is the largest provider of open source PHP packages for the Laravel community. They do a bunch of other stuff. They're an agency, and they do web development. So definitely check out Spatie.be. I'm on the Spatie open source page right here, and really awesome. You can see some of their packages. They have so many packages. They're really working hard to share and create packages for the community. They also have general PHP and JavaScript packages. Now, something to denote there, no, I don't think bad many people follow along this or follow through with this, but I highly suggest they basically share their packages free.

And their one sort of stipulation with that is postcardware. And all that means is you send them a postcard of where you're at. So if you're in Los Angeles, you're in New York, you're in a... I don't know. Those are all in the United States, but wherever you are, right, get a postcard, maybe draw a postcard, grab a postcard from your local supermarket or something and send them over a postcard. Right? It's going to cost you a couple bucks maybe. And then again, you're using it package that's completely free there for you to go in and it's going to save you hours and hours of time. So keep that in mind with Spatie and the packages when using that.

So Spatie Laravel sitemap. Let's start with this. Why do you need a sitemap? Well, there's a sort of some conflicting ideas about sitemaps on whether you need them or not anymore. In the old days everybody said, got to have a sitemap, got to have your sitemap.xml file. And that's the only way that the bots or Google's going to know to find you. That's not necessarily the case anymore, and if you have a really simple site and a very clear navigation or link structure, Google's going to find you, it's going to scrape your pages or being, or whoever you're after. Probably you're after Google for that scraping, and then you're going to index you and you're going to show up on the result. Now it might take a while, but you can go and do that. Now to speed up that process, some people say is create an XML, sitemap.xml file and submit it to Google, and then they'll know all the pages to scrape. Okay? Sure. So you can go and do that. Some of the people say, if you have a more complex site, meaning lots of links, not just your 10 or 20 sort of basic links, but pages within pages within pages or a complex URL structure or something that Google might not understand, it's a good idea to make a sitemap.

So I'm one of those believers that really believe in generating sitemaps for my sites. So I tend to go and say, ah, I need to make a sitemap, and that's what I'll go and do. So how do I do that? I can manually go in and generate an XML file, or I can try to use a tool on the internet to maybe scrape my site and then generate an XML file. But Laravel Sitemap makes it even easier for us to do that. And you can see it has over 48,000 downloads. So quite a few people believe, I guess, in the sense that you needed sitemaps, and you need to do it with Laravel Sitemap, obviously if you're using the Laravel install, right? So let's jump in here and talk about this a little bit more.

I assume, you know, Laravel already, and you have some basic understanding. You maybe follow it along with some of the basic series here on Code Time. You understand base cron, create, read, update, and delete, and you have a model set up and that's why you either have pages or posts or something like that, whatever your model be, that you want to go and get that to be indexed. So how does that work? You install this package and then just a couple of lines of code, and you can generate a sitemap. It's really just this, these two. And if you want to add files to that or other links to it, you can set it up manually here, and you're pretty much configured.

So what I'm going to do is just show you a brand-new Laravel install and what it would be to make a couple of routes that'll be sort of just test pages. So you can kind of see what it's doing, and I'm going to have it set this up. And then I'm going to take it a step further and set this up into a console command, which then will be able to be set up on a cron. And we'll set it up in the kernel to run maybe once a week. It depends on how often. Okay. So let me slow that down for you. The idea there is a cron, basically a task that's going to run at a given multiple times at every minute, let's say. So every minute, basically a request or a check is going to run, and that's what the cron's going to do. And then it's going to look at that kernel file. And it's going to say, Hey, do I need to run any commands?

So once you have this set up, you could be sleeping, or you could be doing something else. And it's going to run that given time, if you're not familiar with cron and the kernel that's at Laravel. And I'm going to show you how to go and do that. Then once that goes and runs, it's going to generate that sitemap, and we need to store that sitemap. So we're going to store it on our file system. And Laravel sitemap is going to do that for us, really easy, easy to configure, and then we're done. It's set up. So this is pretty easy series a to follow along with, and Laravel Sitemap by Spatie has made it very, very easy to go and do it. So with that, let's get started and figure our Laravel sitemap with our Laravel install in the next episode.