Example of yii2.0 running in php command line

Source: Internet
Author: User
Tags yii
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.