Example to explain the steps yii2.0 runs in the PHP command line _php instance

Source: Internet
Author: User
Tags yii

The resources in Yii are files related to Web pages, CSS files, javascript files, pictures, videos, and so on, and resources are placed in the Web accessible directory and are directly invoked by the Web server.

Sometimes there are functions that need to be done to plan tasks, so yii can be executed with PHP commands. The yii2.0 is supported by the PHP command line and is operated as follows:

There is a "yii" file (not "Yii.bat") underneath the Yii root directory, and this file is the portal. Then the "commands" directory will default to have a "hellocontroller.php" controller file, this is a demo. Open the controller file, the source code is as follows (delete part of the note):

<?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 echoed.
   * * Public
  function Actionindex ($message = ' Hello World ')
  {
    echo $message. "\ n";
  }
}
   

As you can see from the above, the Controller under the console are inherited while the command line is running, and the controllers in the general "controllers" directory inherit the controller under the Web.

So the steps to perform a yii script on the PHP command line are:

1. Create a new script

Under the "Commands" directory, create a new controller script file in the format "hellocontroller.php".

2. Execute script at command line

At the command line, "/yii the path to the PHP Yii project, the controller name/method name", such as "PHP E:/wwwroot/yii2/yii Hello/index", as shown in the following figure.

It's important to Note that PHP must set the environment variable before it can be used. to find out if PHP has added environment variables, use the "php-v" command to view them.

Three kinds of controller classes in Yii

Base\controller.php this is the base class for the following two
Console\controller.php This is the console controller.
Web\controller.php This is the Web controller

The above content is a small series through an example to explain yii2.0 in the PHP command line to run the steps, I hope you like.

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.