Lesson 2 – Artisan Commands
Artisans are utilities that enable you to perform repetitive tasks with utmost ease. They are actually the bash commands and each of them performs a specific task including database seeding and migration, creating necessary files for authentication and clearing cache. They also help you create controllers, event classes, models, and much more.
The Artisan commands actually make web application development rather easy for you. The following are some of these commands and the explanation of what they do.
First of all, you use the list command to see all the available artisan commands.
php artisan list
Similarly, just write help before any command to learn more about it.
php artisan help list
There are also multiple methods to filter all registered routes using the list command. One of these methods is as under.
php artisan route:list -–method=Get -–method=Post
If you write the above command, you will get all the routes that accept both Post and Get methods at the same time.
You can also run all the Artisan commands by writing a PHP code, especially from your controllers and routers. Take a look at the following code for example.
The above command will put the application into the maintenance mode.
Similarly, it is also possible to create and register a new Artisan command. All you have to do is to write the following command line.
php artisan make:command [command name]
Once you execute this command, it will make a new command’s class inside the app/Console/Commands directory.
You also have to register your newly created artisan command inside the app/Console/Kernel.php class. This class further has a commands property. Now, you can add your class in class array. The following is a sample code you can copy to attain this goal.
protected $commands = [ Commands\[commandName]::class ];
Now, you can go to your console to use your command. For example, the name of your new command is Laravel.
protected $signature = ‘laravel:command’;
Finally, you will always call the handle method inside every class with the signature laravel:command whenever you execute the above command.
In the next lesson, we are going to study Views in Laravel.