Now that we've generated a sitemap, but we know it's working and we know it's working with the route. We want to take some of this code out, or better yet, generate a console command in which we can go in and automate this. Right. That's the cool thing, I think I get most excited about automation. The fact that I don't have to be there when it happens. That these bots or robots, this future where you don't do anything and all the robots do everything, that gets me really excited. That's what we want to set up. In order to do that, if we go to app and then console, you'll see I don't have any commands yet, I can generate a command. It's pretty easy to do that. If you just say, I'll do it with php artisan to show you that, I have an alias setup as art. If you say PHP Artisan and then if you scroll up here, you'll see all those orders and commands.
We're going to say php artisan make:command. So, I'll say make:command. And then the name of the command I want to call it, let's see here, they gave a name. Let's call it SitemapGenerator. Actually, if we scroll down a little bit further, they say customizing the sitemapgenerator. Enabling manually, creating a site map frequency. They called it, generate sitemap. I'll just show you this same way. I'm going to put a space in there. Okay. So, that's going to make a command called, you'll see our commands folder, generate site map. This is just the default.
Now, you can go in and write all this in here, but they've actually made it even easier for you. They actually provide it here. You can just take this, paste it. Of course, you'll need to add the PHP, paste it. Now you have a site map generator. Here's the signature, here's the description and here's the handle. You'll run 'sitemap:generate'; 'Generate the sitemap.'; as the description. Then it's going to go to Sitemapgenerator::create(config('app.url')) and then it's going to writeToFile(public_path('sitemap.xml'));
Okay, well, that's done really. Except, you need to set this up and if you haven't set up 'app.url' well that's not going to work. When you talk about the config, they're talking about the ENV file there. So, let's go and take a look at that. We'll go into our ENV file and here's our APP_URL. I'm going to come in here and write Laravel.test. That's the name of my site that I've set up. Again, I'm using Laravel [inaudible 00:02:43] to do this. Okay.
Jump back to here, I'm going to save this and then I'm going to grab that line right there. Copy it, minus the strings, go back into command line and I'm going to run that command. So, I'll say php artisan sitemap::generate. And it just ran. Okay. So, let's see here, I'm going to delete it and make sure that it's running. We'll go to sitemap.xml I'm going to click delete. Click okay and then I'm going to run this again and you see it got generated that time and here's the latest file. So, that's it, you're set up. Generates a sitemap, hits the 'app.url' writes the (public_path('sitemap.xml')); and you're configured. That's really it. You have a sitemap generator that's ready to go and run.
Now, if you want to take this as a little step further, instead of, again, we went from the routes to the command line. But now we want to set it up with the kernel. If we go to the kernel here, this is where we schedule our commands. In the older way of doing things, you add a reference to the commands here. I think before version 5.4 you needed to do that. If you're using level 5.5 and it's set up the same way, like the way I just showed you, you don't need to go and reference the commands here.
You can just go and un-comment here and it'll say, $schedule->command('sitemap:generate') and then how often you want it to go in and run. And this things, you could write something like daily, or you could say weekly, or you could say something like Thursday. I think it's Thursdays, I think it's with an S on there. And it's set up. Now, if you have questions about what more about this kernel and cron and all that, if you just do a simple search for a Laravel and then kernel scheduler. I'm on 5.5 here. If you scroll down, there's a little section here, which they've nicely put into a table for you.
What's pretty cool, so this format of cron, I've always had a hard time with it. Yeah, it's really not that hard probably. I just never remember which asterisk matches up and what number you're [inaudible 00:05:00] to put in there just didn't make sense. I think one of the reasons why is, it's not clear, you have to really know that, really understand, remember that and it's not that readable. Even when you look at it immediately, it's not like, "Oh, that's what it is."
So, Laravel made its' course, like many things, very, very easy. You can run it every minute, every five minutes. You can also chain these together if you want to. Every 15 minutes, hourly, hourly at a specific hour, daily, daily at a specific hour, twice daily, weekly, monthly, monthly on, quarterly, yearly and time zone specific. So, pretty easy to go in and set up. You also have these, so on weekdays run it, on Sundays run it, on Mondays on Tuesdays on Wednesdays, Thursdays, Fridays, or between a specific time. You can set up when with your own closure, if you really need to go and take it further.
Most of the time I just do it on a specific day or I do it weekly. So, there it is. It's going to run weekly, I don't really care when weekly it's going to run. As I just know, every week it's going to update. If you have something where your content is constantly changing, maybe it changes every day. Well, then run daily. It's a very small command to run basically. It's not going to hit your system hard. I mean, maybe if you have a ton of pages, it might take a little bit longer to go and run and you might see that on your server logs. But I haven't noticed it, I haven't had a problem I'm with it. Obviously, weekly is going to work great for my needs here. So, you can configure it. I want to talk about a couple other things in the next episode and those are some little nuances. That'll be excluding some pages and some other configuration that you might want to go and do. Let's go and take a look at that in the next episode.