Sometimes some functions need to be scheduled, so yii needs to be executed using php commands. Yii2.0 supports the php command line operation as follows:
There is a "yii" file (not "yii. bat") under the yii root directory. This file is the portal. The "commands" directory contains a "HelloController. php" controller file by default. This is a demo. Open the controller file. The source code is as follows (some comments are deleted ):
<? Php
Namespace app \ commands;
Use yii \ console \ Controller;
Class HelloController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @ Param string $ message the message to be echoed.
*/
Public function actionIndex ($ message = 'Hello World ')
{
Echo $ message. "\ n ";
}
}
It can be seen from the above that the Controller under the console will be inherited when running the command line; while the Controller in the "controllers" directory will inherit the Controller under the web.
Therefore, the steps for executing the yii script in the php command line are as follows:
1. Create a script
Create a controller script file in the "commands" directory in the "HelloController. php" format.
2. Execute the script on the command line.
In the command line, enter "php yii project path/yii controller name/method name", for example, "php E:/wwwroot/yii2/yii hello/index", as shown in the following figure.
Note that:Php must set the environment variable before it can be used. To see if php has added environment variables, run the "php-v" command.