در حال بارگذاری ...

Creating a Console Command0:00

Check this out. Defining signatures for artisan commands has never been simpler. I'm going to make a new console command, and we'll just call it showGreeting. Something very simple to demonstrate this. Well, this will add it to app/Console/Commands/showGreeting.php. But now, if we take a look at the makeup of this, it's a little cleaner. Actually, it's a lot cleaner. Before, it was called fire, but that's not really consistent with most of Laravel's API these days. And also, you'll see that there's no section for defining your arguments and your options. And if you remember how it was done in the past, well, it was kind of un-Laravel-like, wasn't it?

Registering the Command0:56

Well, now, it has been drastically improved. Let's begin by saying this info, hello there, Joe. Okay, well, of course, this doesn't work just yet, because we have to register it. We do that in app/Console/Kernel.php, and I will duplicate this. Finally, if we switch back to showGreeting, right up here, we can define it. How about laracasts.greet? And actually, on that note, real quick, when you define it, if you also want to do the signature in the process, then you could say command. And I'll show you that in just a second. But anyways, at least to start, if I run php artisan, and we scroll up, there it is.

Introducing Command Signatures1:29

And I'll show you that in just a second. But anyways, at least to start, if I run php artisan, and we scroll up, there it is. So let's try to run that. And it works. Okay, but now we want to work on the signature. Well, we do all of that right here. Notice we no longer have a property of name. It's actually a property of signature. You define how you call it, plus the arguments, if relevant, plus the options, if relevant.

Adding Required Arguments1:52

You define how you call it, plus the arguments, if relevant, plus the options, if relevant. That's all contained here. So for example, if I want to accept a name, then we do it like this. Notice how it's almost like defining a route. Very, very similar. And if you think about it, that makes perfect sense. Defining a route URI, a structure for that, should be similar to how you would define an artisan command endpoint. So anyways, if we were to run this, it's going to fail,

should be similar to how you would define an artisan command endpoint. So anyways, if we were to run this, it's going to fail, and it'll let us know, hey, not enough arguments. Okay, we'll pass that through. And now it got accepted, but of course, we didn't reference that argument anywhere. Let's do that now. I'll say, hello there, this argument name. Okay, now if we run it, that works. But now if we bring it back to no arguments, well, we get that error again. What if we want a default?

Optional Arguments and Defaults2:41

But now if we bring it back to no arguments, well, we get that error again. What if we want a default? Or what if we want it to be optional? Well, very easy. If we scroll up, I could use ?, just like my route.php file, to say I want a name argument, but it's not required. All right, we run it, and now it didn't fail on us, but we didn't get any output. Maybe we could instead set a default. Well, just use the = sign. Done.

Defining Command Options3:08

Well, just use the = sign. Done. Run it again, and there you go. Or, like I said, we can override it. Now, what about options? Same thing. How do you call an option when you run it from the terminal? Well, you do something like foo=bar, right? Same thing here. How about the actual greeting will be hi.

So cool, right? You have to admit, this is infinitely more intuitive and simple than the way we were doing it before. And mostly, that's all there is to it. So just to finish up, let's delete that file and temporarily comment this out. And just to show you how you can define it on the fly, you could say make:console showGreeting, and then you could add the signature here. You do command. I wish it was actually signature. That would make more sense to me.

I wish it was actually signature. That would make more sense to me. But anyways, the command will be lyricass greet, and then we expect a name. That'll be our basic signature. So now, you'll see it creates that, but it also populates this for you. So now, just bring that back. But yeah, that's all there is to it. So now, once again, we could say lyricass greet, and it'll fail because we don't have an argument. We can pass one in.

an argument. We can pass one in. How about one of our lyricass users? And remember, we have no handle method logic here, so it'll be blank. But you now know how simple it is to define your arguments and options for artisan commands.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟