Project Setup With Composer0:00
Okay, let's start from scratch with the obligatory hello world commands. So I will create a folder, command_series, and if we cd into that folder, we can get started. Now, of course, the first thing that we will need here is our composer.json file. So we want to pull in that Symfony console component, right? So require symfony/symfony console and anything 2.0. And that's good. So let's run composer install to pull that in. All right, so with that done, step two is to create the file that we will actually execute. So for example, with the Laravel installer, the file name would be Laravel.
Create Executable Script0:31
All right, so with that done, step two is to create the file that we will actually execute. So for example, with the Laravel installer, the file name would be Laravel. Or with Homestead, we could create a Homestead file. So let's do that now. And I'm just going to keep it simple and call this Laracasts. Now we're going to use PHP to set this up, however, I do want to make sure that I specify the environment is in fact PHP. Because remember, we are executing this from the command line, of course. So as with anything, the first step is to pull in our autoloader. And we get that out of the box using composer.
Bootstrap Console Application1:05
So as with anything, the first step is to pull in our autoloader. And we get that out of the box using composer. So require vendor/autoload.php. Now we can set up a new console application. New Application. And do notice the full namespace path here that was automatically imported. Now we could keep it like this if we want to. And in fact, that's what we'll do. But we can also pass in the name of our console app as well as the version number. So let's change this to app.
Register Command and Argument1:31
But we can also pass in the name of our console app as well as the version number. So let's change this to app. And then we can register a new command. Now we can do this inline here, which is great for simple stuff. Or if you want a dedicated class, then you can do that as well. And we're going to review both options in this series. But let's start with the basics. So let's go ahead and register a new command. And we will call this sayHello2. Next we need to add an argument.
And we will call this sayHello2. Next we need to add an argument. That way, ideally, what I want to do is say laracas sayHello2. And then I pass in my name. So we do that using the addArgument method here. The argument name will be name. And next we just need to set the code that will be executed. So I will pass a closure here. And then we want to make sure that we accept the input. So I'm going to use InputInterface.
And then we want to make sure that we accept the input. So I'm going to use InputInterface. Once again, this all comes from the console component. And then we also need the output. Anything that we would return. Like so. So now to start, let's just say output. We're going to write a new line and we'll say hello world. And then we will expand on this. Now what we have here still is not enough.
Make Script Executable2:42
And then we will expand on this. Now what we have here still is not enough. Yes, we've registered a new command. But well, let's just try it. Notice we get permission denied. This is not working because of two reasons. One, we need to make sure that this file is executable. So let's do that. chmod +x on that file. All right.
Change mode to be executable on that file. All right. But if I try it again, notice that we get nothing at all. So it did execute it, but we just get no output whatsoever. And that's because we need to start up the application. We do that by saying app run. So now let's try it again. We trigger it. And there you go. So notice that we have these default arguments and options.
And there you go. So notice that we have these default arguments and options. And again, this comes from Symfony's console component. So it's included out of the box. But more specifically, notice that we have our sayHelloTo command. Let's try it. laracast say hello to. And we get hello world. So what if we've never used this command before and we don't know what arguments or options it accepts?
Improve Help and Descriptions3:50
So what if we've never used this command before and we don't know what arguments or options it accepts? Well, we can say help and then pass in the name of the command. So we run it. And notice that yes, it tells us it requires a name argument, but it doesn't give us any more information. We can fix that. When we add our argument here, we can specify whether or not it's required. And by default, it's not required. So in this case, I do need to make sure that you give me your name.
And by default, it's not required. So in this case, I do need to make sure that you give me your name. So we will reference a different class here, Symfony\Component\Console\Input\InputArgument. Now I can say inputArgument required. And this has made a big difference now. If we were to run this again, notice that we will be told, hey, you didn't pass in enough arguments. But if we change that to optional, well, you can pass it or you don't have to. We don't care either way. But still, we haven't fixed this issue where the help information is lacking.
We don't care either way. But still, we haven't fixed this issue where the help information is lacking. Well, we just add that as the third argument here, your name. So now if we run it again, we have some extra information here. And the same is going to be true if we trigger Lericast in general. Notice how it says console tool. That's not really useful. So let's modify that. new application, Lericast demo, and the version number is 1.0. Now if we run it again, that has been replaced accordingly.
New application, Lericast demo, and the version number is 1.0. Now if we run it again, that has been replaced accordingly. So what else do we need in terms of documentation? Well, here's the actual command, but once again, it doesn't really tell us what the command does. So let's fix that. When we register sayHello2, I'm going to set a description. Or a greeting to the given person. Alright, we run it again, and that's all there is to it. So moving on, yes, we can do sayHello2, and I'll pass in my name here, but we're not making
Use Arguments and Style Output5:42
Alright, we run it again, and that's all there is to it. So moving on, yes, we can do sayHello2, and I'll pass in my name here, but we're not making use of that just yet. We're just spitting out hello world. Let's change that. Because we've referenced these input and output interfaces, it's really easy to fetch those arguments and options. Like this, name is going to be input.getArgument('name'), and then we reference the name that we assigned. And in this case, we just called it actually name.
we assigned. And in this case, we just called it actually name. So now we could say hello, ',', and then pass in their name. Let's try it, and there we go. Hello Jeffrey, Hello Jane. Now in this case, what we spit out is a little boring. If we instead want to style it in some way, give it a different color, well, we can do that as well. For example, we can wrap these strings within these HTML-like tags. So I could say comment, and then close it out as well.
For example, we can wrap these strings within these HTML-like tags. So I could say comment, and then close it out as well. Now notice if we run this again, we have some different styling here. And we also have things like info, and a couple of others. So going back to Laravel, if you've ever created a new command, you know that there are these helpful methods like info and comment. Well, once again, those are just little wrappers that make the process of doing these sorts of tasks that much easier. So why don't we clean this up just a little bit. We'll say message equals hello, ',', and then input.getArgument('name').
So why don't we clean this up just a little bit. We'll say message equals hello, ',', and then input.getArgument('name'). That way, we can remove that entirely and simply wrap this like so, info, message, and then close it out. All right, try it one more time, and it still works. All right, so that does it for our opening lesson here. And actually, we've learned how by using Symfony's console component, this stuff just becomes incredibly easy. There's really not much to it at all.
There's really not much to it at all.
