تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Switching to Command Classes0:00

In the last episode, I gave you the basic rundown for registering a quick command, accepting some arguments, and then performing some kind of action. Now, this is actually perfect for simple stuff. But in real life, it probably wouldn't be practical to store your entire command within a closure. And what if we had multiple commands? Well, we would have to repeat all of this within the same file, and then that gets sort of muddy. So instead, in this video, let's swap out this existing code, but instead use a class. And I'll show you what that looks like.

Creating Command Class File0:26

So instead, in this video, let's swap out this existing code, but instead use a class. And I'll show you what that looks like. Let's open up my sidebar here, and we're going to add a new directory called source. Here is where we will store any and all commands. So in this case, we have our sayHello command. Let's add that sayHello command. And our namespace will be a typical ACME. Next, I want to make sure that we extend Symfony's Command class. Now, before we reference this class, I need to make sure that we can autoload it. So from our composer.json file, I will specify that I want PSR-4 autoloading.

Setting Up PSR-4 Autoloading0:57

Now, before we reference this class, I need to make sure that we can autoload it. So from our composer.json file, I will specify that I want PSR-4 autoloading. And we'll say our root namespace here is called ACME, and that is located within the source directory. So now that I've updated that, I can run composer dump-autoload, and we're good to go. So let's see what we need to do. The first step is I need to register a sayHello2 command. I got to set the description and add an argument. So let's tackle that now.

Configuring the Command1:27

I got to set the description and add an argument. So let's tackle that now. Right here, we will mostly have two main methods. We'll have a configure method, and this is where we can apply any setters or accept some arguments. And then next, we will have our actual execute method, where we would process the command however we need to. All right, so let's set our command's name. Let's set name. And we called that sayHello2.

Let's set name. And we called that sayHello2. Next, we're going to set the description, and let's go ahead and snatch that and paste it in. And next, we need to add our argument. So once again, I can just move that over here. And for a quick recap, the name of the argument, is the argument optional or required? Let's make it required now. And then a description of the argument. So that should do it for our configuration.

Implementing Execute Logic2:15

And then a description of the argument. So that should do it for our configuration. Next, when we execute this command, we will have access to our input interface. We'll call that input, and also output interface for the output. So like we reviewed in the last lesson, we can fetch our argument values or any options that the user provides using input here. And then if we want to output anything to the console, we can use output. So it's really no different than what we had right here. So next, let's take the body here and move it over. And that's it.

Registering and Running Commands2:46

So next, let's take the body here and move it over. And that's it. It's just not that hard at all. So let's go back here. We can remove all of this entirely. And instead, I'm going to add a new command, app add a new sayHello command. And notice that it was imported at the top here. So we initialize our application. We add any number of commands. So for example, if I had three different commands, I would do something like this.

We add any number of commands. So for example, if I had three different commands, I would do something like this. And then finally, when you're ready, you run the application. And by the way, notice up here, because we moved this to a class, we can remove those entirely. So with that, I think we are ready to try this out. From the console, we will run lericas, sayhello to, and I will pass my name. And whoops, in this case, it looks like we're trying to reference a class that we didn't correctly import. So let's go back, and here you can see I have inputArgument, but I didn't import that.

correctly import. So let's go back, and here you can see I have input argument, but I didn't import that full class. So if we do that now, let's run it again, and it should work. Great. Hello, Jeffrey. Hello, Jane. So now that we've translated this to a class, let me quickly show you some other things you can do. We know about arguments, but what about optional type things?

Adding Options with Defaults4:01

you can do. We know about arguments, but what about optional type things? Well, we can use addOption. For example, if we continue with this really simple Idea for a command, maybe you want to override the default greeting that is used. All right. Well, we want an option for greeting. Next, we could provide any shortcut if we want. I'm going to set that to null for now. And further, we can use inputOption here to specify if any value is expected.

I'm going to set that to null for now. And further, we can use input option here to specify if any value is expected. Remember, we could use an option as a simple flag. If a user specifies some flag, then that's all we need, and we don't even need a value from them. In those cases, value none would be fine. In this case, though, we're going to go with value optional. And then finally, once again, a description for the option. Override the default greeting. And that's it.

Override the default greeting. And that's it. However, because this is optional, it might make sense to add a default value for the greeting. So we can do that as the final argument, hello. But that's it. Now we can grab that value. Right down here, let's reformat this just a bit. We'll use printf here. We want greeting, comma, and then the person's name.

We'll use printf here. We want greeting, comma, and then the person's name. And then the values that we will bind will be input()->getOption() this time, greeting. And then finally, input()->getArgument(), name. All right. Let's try it out and see what we get. Larrakass, say hello to, John Doe. You'll see that we use the default here. But if I want to override the greeting, I can use hi. Or a different version would be something like this.

But if I want to override the greeting, I can use hi. Or a different version would be something like this. Either one will work here. All right. And that'll do it for this episode. So now you've not only learned how to create a simple command, but you've also learned how to translate that to a console command class.

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