Example of how to run yii2.0 in php command line step _ php instance

Source: Internet
Author: User
Yii resources are files related to Web pages. They can be CSS files, JavaScript files, images or videos. Resources are placed in accessible Web directories and directly called by Web servers. This article explains how to run yii2.0 in the php command line through an example. If you are interested in yii2.0php, learn about the resources in Yii as files related to Web pages. They can be CSS files, javaScript files, images, videos, and other resources are stored in accessible Web directories and directly called by Web servers.

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 ):

<?phpnamespace 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.

Type "php yii project path/yii controller name/method name" in the command line, for example, "php E:/wwwroot/yii2/yii hello/index", as shown in.

It should be noted 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.

Three controller classes in Yii

Base \ Controller. php: The following two base classes
Console \ Controller. php this is the console Controller
Web \ Controller. php: web Controller

The above section describes how to run yii2.0 in the php command line through an example.

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.