Tutorial on planning tasks for PHP programs using YII Framework

Source: Internet
Author: User
Tutorial on planning tasks for PHP programs using YII Framework

1. after you create a webapp through yiic,
Yiic is generated under webapp/protected. php, because it is a command line application, the yiic here. php is actually the index of webapp. like php, the command line entry file.

2. open the yiic file, add a row of settings, and add the path of the commands directory to yiic. in this way, yiic can find the command file under the commands Directory. the modified code is as follows, red is the new code:

123456

3. configure the components required in product/config/console. php, such as database connection.

Configure main/console. php and set the import path and db connection. This part is similar to main. php.

12345678910111213 'DB' => array ('ononstring' => 'MySQL: host = localhost; dbname = testdrive ', 'emulateprepare' => true, 'username' => 'root ', 'password' => '', 'charset' => 'utf8 ',),

Configure the class libraries and extensions that you want to automatically load at the same time:

123456789 'Import' => array ('application. components. commonclass', 'application. models. * ', 'application. components .*',),

4. inherit from CConsoleCommand and write it into your own command class. Yii provides two methods for execution. if you execute a single task, write it directly in the run method, you can also write your Controller in the same way and add actionXXX.
This example uses the second method, namely web program development, and adds the actionXXX method to the CConsoleCommand class to execute the program.
We create a file in the commands directory to execute the task we want to execute. now we name it TestCommand. php.


12345678910111213141516171819 ClassTestCommandextendsCConsoleCommand {public $ Action = 'index'; // default Action/*** write your code logic here. */PublicfunctionactionIndex () {// write your code logic here }}

5. OK, basically finished,

Open the cmd command line window, for example, switch to the protected directory, and run

Yiic Test index


Then we started to execute the PHP script!


Based on the principle that the core should not be modified, the above tutorial is just an example. in the actual development process, a new class should be created to inherit Yiic for the second step, so as not to cause trouble for future core upgrade, and cause security risks

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.