This article brings you relevant knowledge about Laravel, which mainly introduces issues related to running command line scripts. There is a separate directory in Laravel, which is the Console directory. It is used to store script files. Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended learning: Getting started with Laravel
We saw that there is a separate directory in Laravel, which is the Console directory. It is used to store script files. This script file generally refers to the command line script we execute through the php command, which has such a function in many frameworks. For modern application development, some time-consuming functions such as data statistics, data export, queue processing, and some automated back-end running programs need to be executed using this command line script.
In the current framework directory, we execute php artisan in the root directory, and you can see the command line help information. Here is a list of all existing Command line script. In the first article, we came into contact with two of these commands.
# php artisan key:generate # php artisan serve
One of their functions is to generate a unique Key that needs to be used for encrypted cache, etc., and the other is to run a simple server that comes with it. We can see from the script name that the script can be separated by a :, and there are large categories before the colon, such as cache:xxx related and make:xxx related. Cache is related to processing some cache information, while make is related to creating some files we need. For example, to create a controller, you can use make:controller, and to create a data model, you can use make:model.
Regarding these default built-in scripts, we will learn about them when we learn the relevant content.
Customizing a script is very simple. We can use the make:command command to generate a command line script.
# php artisan make:command test1 Console command created successfully.
At this time, a test1.php file will appear in the app/Console/Commands directory. Open this file, we need to make some modifications.
/** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name'; /** * The console command description. * * @var string */ protected $description = 'Command description';
signature is used to set the name of the current script, and description is used to define the comment description of the script. Where are they used? In fact, signature is the name we need to use when running this script through php artisan. For example, if we directly execute php artisan now, we will see the following executable command line script appear.
command command:name Command description
Of course, using this default name is not a good idea, so we can modify these two properties.
/** * The name and signature of the console command. * * @var string */ protected $signature = 'ZyBlog:Test1'; /** * The console command description. * * @var string */ protected $description = '硬核测试1';
If we run php artisan again at this time, we can see the information we defined.
ZyBlog ZyBlog:Test1 硬核测试1
It is very simple to run this script.
# php artisan ZyBlog:Test1
Of course, we haven’t done anything yet, so there will be no output. Next, we combined the receiving parameters and output information. To receive parameters, we need to define the parameters and options we want to receive in signature. Remember the article we talked about before about how to receive script parameters and option information in PHP? Laravel has already encapsulated these, so you don't need to use those functions for reception and processing, just use them directly. Students who need to review can go to [How to obtain PHP command line parameters] mp.weixin.qq.com/s/dFuGaM1JT… for review or learning.
protected $signature = 'ZyBlog:Test1 {a=1} {--b=*}'; /** * Execute the console command. * * @return int */ public function handle() { echo "欢迎进来测试!", PHP_EOL; print_r($this->arguments()); // Array // ( // [command] => ZyBlog:Test1 // [a] => 1 // ) print_r($this->options()); // Array // ( // [b] => Array // ( // [0] => 2 // ) // [help] => // [quiet] => // [verbose] => // [version] => // [ansi] => // [no-ansi] => // [no-interaction] => // [env] => // ) echo $this->argument('a'); // 1 print_r($this->option('b')); // Array // ( // [0] => 2 // ) return 0; }
In the handle() function, we can write the function code that the current script needs to execute. Among them, the parameter information of the script can be received through arguments() and argument(), and the option information of the script can be received through options() and option(). Regarding parameters and options, we have also explained them in previous articles, so we won’t go into details here. Everything is based on the basics.
For parameters and options, Laravel’s underlying call is actually symfony’s Console component. In symfony/console/Input/ArgvInput.php, we can see Go to the code below.
public function __construct(array $argv = null, InputDefinition $definition = null) { $argv = $argv ?? $_SERVER['argv'] ?? []; // strip the application name array_shift($argv); $this->tokens = $argv; parent::__construct($definition); } // …………………… // …………………… protected function parse() { $parseOptions = true; $this->parsed = $this->tokens; while (null !== $token = array_shift($this->parsed)) { if ($parseOptions && '' == $token) { $this->parseArgument($token); } elseif ($parseOptions && '--' == $token) { $parseOptions = false; } elseif ($parseOptions && 0 === strpos($token, '--')) { $this->parseLongOption($token); } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { $this->parseShortOption($token); } else { $this->parseArgument($token); } } }
Obviously, in symfony, argv is also used to get parameters and options, and then they are put into input variables and passed down. This input variable is very important, and we will also come into contact with it later when we learn request-related content. Later, in our execution code, that is, using argument() or option() in the handle() method of Command, the data in this input is obtained. We can see them from breakpoint debugging.
So how does Laravel execute the handle() function? First, call the laravel/framework/src/Illuminate/Foundation/Console/Kernel.php file through the artisan file. In the handle() method in Kernel.php, symfony/console/Application.php will be called, and then enter laravel/framework Execute the execute() method in /src/Illuminate/Console/Command.php and call our customized handle() method through callback.
Note that at the bottom of laravel/framework/src/Illuminate/Console/Command.php, the method in console/command.php under symfony is still called.
The entire call chain is very long, but it can be clearly seen that our Laravel is indeed a shell based on Symfony. And it's not just the command line. In terms of Web requests, Symfony still plays a crucial role at the bottom level.
Recommended learning: Laravel video tutorial
The above is the detailed content of Let's talk about Laravel running command line scripts. For more information, please follow other related articles on the PHP Chinese website!