国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial yii scheduled tasks

yii scheduled tasks

Aug 08, 2016 am 09:31 AM
action application console

Yii框架自動生成的Web應用骨架的目錄里面有連個腳步文件,yiic和yiic.bat。

yiic是Unix/Linux平臺用的,yiic.bat是windows平臺用的。如果要查看腳本的幫助可以進入到腳步所在的根目錄,然后執(zhí)行yiic help,他會列出所有可用的命令,里面包括Yii提供的系統(tǒng)命令和用戶自定義的命令。

如果要知道如何執(zhí)行一個命令可以執(zhí)行以下命令:

1	yiic help
如果要執(zhí)行一個命令,可以使用如下格式:

1	yiic [parameters...]
1、創(chuàng)建命令

控制臺命令都是以類文件的方式存儲在 CConsoleApplication::commandPath 指定的目錄。默認是存儲在 protected/commands 。

每個類必須繼承自 CConsoleCommand 。類名的格式是 XyzCommand ,命令的名字首字母大寫,xyz才是命令本身。

可以通過配置 CConsoleApplication::commandMap ,命令類可以有不同的命名約定和不同的目錄。

創(chuàng)建一個新命令你可以覆蓋 CConsoleCommand::run() 或者寫一個或多個action.

覆蓋父類的run方法格式可以是:

1	public   function   run( $args ) { ... }
當執(zhí)行一個命令的時候,run方法將會被調(diào)用,任何加在調(diào)用命令后面的參數(shù)將會賦給$args。

在命令內(nèi)部可以用 Yii::app() 調(diào)用這個控制臺的實例。

從1.1.1版本開始,可以創(chuàng)建一個全局的命令,被在同一臺機器上的所有的Yii應用所共享。為了達到這樣的目的,你需要定義一個名為

YII_CONSOLE_COMMANDS 的環(huán)境變量,指向一個已存在的目錄,然后把這個全局的命令類放在這個目錄里面。

2、控制臺命令Action

一個控制臺命令action就是一個控制臺命令類的一個方法。

方法名的格式: actionXyz ,action名的首字母大寫,xyz才是被調(diào)用的action本身。

執(zhí)行一個action的命令格式:

1	yiic --option1=value1 --option2=value2 ...
后面的option-value對將會賦給這個action方法的參數(shù)。如果你給出了option名而沒有給出對應的值,那么這個option將會被認為是boolean值true。

action的參數(shù)也可以聲明一個數(shù)組類型,如:

1	public   function   actionIndex( array   $types ) { ... }
調(diào)用它的命令是:

1	yiic sitemap index --types=News --types=Article
最終命令調(diào)用是: actionIndex(array('News', 'Article'))。

從1.1.6開始,還支持匿名參數(shù)和全局選項。

匿名參數(shù) 指的是不按正常選項參數(shù)格式(the format of options)的命令行參數(shù),比如: yiic sitemap index --limit=5 News ,News就是一個匿名參數(shù)。

要使用匿名參數(shù),action必須聲明一個 $args變量,比如:

1	public   function   actionIndex( $limit =10, $args = array ()) {...}
$ args 會接收到所有可用的匿名參數(shù)。

全局選項 (Global options)指的是一個命令行選項被這個命令的所有action所共享。

比如:一個命令有好幾個action,我們想在每個action里面都有一個名字叫 verbose 的選項,我們可以在每個action方法里面都聲明一個叫 $verbose 的參數(shù)。

一個更好的做法是把它聲明成這個命令類的公共成員變量( public member variable ),這樣 verbose 就會成為一個全局的選項。

1	class   SitemapCommand extends   CConsoleCommand
2	{
3	public   $verbose =false;
4	public   function   actionIndex( $type ) {...}
5	}
這樣就可以執(zhí)行一個帶 verbose 選項的命令:

1	yiic sitemap index --verbose=1 --type=News
3、退出代碼

執(zhí)行命令的宿主機器可能需要檢測我們的命令執(zhí)行成功與否,它可以通過檢測命令最后退出是返回的退出碼來識別。

退出碼是介于0到254之間的整型值 ,0表示這個命令執(zhí)行成功,非0表示這個命令執(zhí)行期間出現(xiàn)錯誤。

你可以在action或者是run方法里面通過一個退出碼來退出你的應用程序。

比如:

1	if   ( /* error */ ) {
2	return   1; // exit with error code 1
3	}
4	// ... do something ...
5	return   0; // exit successfully
假如沒有返回值,將會有一個默認的0被返回

4、定制控制臺應用

默認的控制臺應用配置位置: protected/config/console.php 。

任何 CConsoleApplication 的公共屬性都可以在這個文件里面配置。

這個配置文件類似于普通的web應用的配置文件。

Customizing Console Applications 

By default, if an application is created using the  yiic webapp  tool, the configuration for the console application will be  protected/config/console.php . Like a Web application configuration file, this file is a PHP script which returns an array representing the property initial values for a console application instance. As a result, any public property of  CConsoleApplication  can be configured in this file.

Because console commands are often created to serve for the Web application, they need to access the resources (such as DB connections) that are used by the latter. We can do so in the console application configuration file like the following:

return array(
    ......
    'components'=>array(
        'db'=>array(
            ......
        ),
    ),
);
As we can see, the format of the configuration is very similar to what we do in a Web application configuration. This is because both  CConsoleApplication  and  CWebApplication  share the same base class.

文章參考: http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.console

----------------------------------------------------------------------------------------------------------------------

一篇文章:

使用YII框架進行PHP程序的計劃任務教程

1.當你通過yiic創(chuàng)建一個webapp應用后, 
會在 webapp/protected/下生成yiic.php, 由于是命令行應用,所以這里的yiic.php其實就是與webapp下的index.php一樣的,命令行入口文件。

2.打開yiic文件,添加一行設置,將commands目錄的路徑添加到y(tǒng)iic中,這樣,yiic就能夠找到commands目錄下的命令文件了,修改后的代碼如下,紅色為新加入代碼:

1
2
3
4
5
6
$yiic =dirname( __FILE__ ). '/http://www.cnblogs.com/yii-read-only/framework/yiic.php' ;

$config =dirname( __FILE__ ). '/config/console.php' ;

@putenv( 'YII_CONSOLE_COMMANDS=' . dirname( __FILE__ ). '/commands' );

require_once ( $yiic );

或者是:

 配置好,要執(zhí)行的頁面。本文為 protected/commands/crons.php

run();
?>

3.配置好produ ct/config/console.php里面需要用到的組件,像數(shù)據(jù)庫連接。

配置main/console.php,設置import路徑,以及db連接,這部份與main.php類似。

php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array (
        'basePath' => dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . '..',
        'name' => 'My Console Application',
        'import' => array (
                'application.models.*',
                'application.components.*',
                'application.components.base.*',
                'application.components.imgthumb.*',
                'application.models.form.*',
                '等等,導入所要的類包'
        ),
        'components' => array (
                // Main DB connection
                'db' => array (
                        'connectionString' => 'mysql:host=localhost;dbname=數(shù)據(jù)庫名稱',
                        'emulatePrepare' => true,
                        'username' => '數(shù)據(jù)庫名稱',
                        'password' => '數(shù)據(jù)庫密碼',
                        'charset' => 'utf8',
                        'tablePrefix' => 'company_'、//表前綴
                ),
                'log' => array (
                        'class' => 'CLogRouter',
                        'routes' => array (
                                array (
                                        'class' => 'CFileLogRoute',
                                        'levels' => 'error, warning'
                                ) 
                        ) 
                ) 
        ) 
);
4.繼承CConsoleCommand寫入自己的命令類,Yii提供了兩種方式去執(zhí)行, 如果你執(zhí)行單一的任務,直接在run方法里面寫,另外一種 就是同寫你的Controller(控制器),增加actionXXX即可。 
本實例采用第二種方式,即采用web程序開發(fā)的方式,在基礎了CConsoleCommand的類中添加actionXXX方法來執(zhí)行程序。 
我們在commands目錄下創(chuàng)建一個文件,來執(zhí)行我們要執(zhí)行的任務,暫且命名為TestCommand.php 。


  

    
      4,打開你的linux命令窗口,創(chuàng)建自動任務。至于windows系統(tǒng) ,是計劃任務(win系統(tǒng),可以谷歌如何操作),下面只講linux系統(tǒng)。
    
  


  
crontab -e
##然后輸入
1 * * * *  php /具體地址/protected/commands/crons.php Test >>/具體地址/protected/commands/test.log
##上面命令說明,每分鐘執(zhí)行Test任務一次,把日志保存在test.log下

  
至此,自動化任務已完成。


  
windows下面計劃任務:


  
schtasks /create /sc minute /mo 1 /tn "taskPhp" /tr  "php F:\xampp\htdocs\php\yiiblog2\protected\commands\crons.php  CInsert insertData"


  
刪除計劃任務 schtasks /delete /tn "taskPhp"


  
每個1分鐘就執(zhí)行CInsert命令中的insertData方法。


  

    參考了:    http://www.cnlvzi.com/index.php/Index/article/id/124
  


  

    http://www.yiibase.com/yii/218.html
  


  

    http://www.yiiframework.com/wiki/221/cronjobsyii/
  


  

    http://986866294.blog.163.com/blog/static/1651222522013571578115/

 

The above introduces the yii planning task, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does console mean? What does console mean? Sep 05, 2023 pm 02:43 PM

Console means console. It is a device or software that interacts with a computer system. It is usually a device with a keyboard and a screen for inputting and outputting information. The console was originally Used for large computer systems, and later also applied to personal computers and servers, it can help users manage and maintain computer systems, as well as install operating systems and applications, debug programs, etc.

Pre-orders open for new Nintendo Switch Lite refresh Pre-orders open for new Nintendo Switch Lite refresh Jun 29, 2024 am 06:49 AM

Nintendo has opened pre-orders for the latest version of the Switch Lite (curr. $189.99 on Amazon). However, the device is not available to order globally just yet. To recap, the company presented the Switch Lite Hyrule Edition almost two weeks ago d

Clear console output using Console.Clear function in C# Clear console output using Console.Clear function in C# Nov 18, 2023 am 11:00 AM

Use the Console.Clear function in C# to clear the console output. In C# console applications, we often need to clear the output information in the console in order to display new content or provide a better user experience. C# provides the Console.Clear function to implement this function, which can clear the output in the console and make the interface blank again. The calling format of the Console.Clear function is as follows: Console.Clear(); This function does not require any input

MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev Sep 01, 2024 am 06:30 AM

If you purchased the MagicX XU Mini M recently, this news might come as a surprise. A hardware and software teardown of the newly released handheld console revealed that the advertised RK3562 CPU is, in fact, a lower-specced, older RK3326 processor.

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black Sep 04, 2024 am 06:51 AM

DJI has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

Nintendo announces new Switch Lite refresh before Switch 2 release Nintendo announces new Switch Lite refresh before Switch 2 release Jun 20, 2024 am 09:41 AM

Nintendo presented plenty of games yesterday during its most recent Nintendo Direct event, an overview of which we have provided separately. Additionally, the company also announced a new version of the Switch Lite (curr. $194.93 on Amazon), possibly

what does console mean what does console mean Aug 09, 2023 pm 04:21 PM

A console is a console, an interactive interface used in computer programs to input and output text or commands. In different operating systems and development environments, the console may have different appearances and functions. Usually a text interface that provides a command line interface or command line prompt, allowing the user to enter commands through the keyboard and display the output of the program.

What is the principle of python WSGI Application? What is the principle of python WSGI Application? May 19, 2023 pm 01:25 PM

The python environment this article relies on is: What is WSGI? WSGI is also called the web server universal gateway interface, and its full name is webservergatewayinterface. It defines a standard for how web servers and web applications should communicate and handle http requests and responses in Python. Note that it is just a protocol, or a specification or standard. You don’t have to follow this standard. Just like the web server we wrote in the previous article. WSGI is also divided into applications and server gateways. Among them, the well-known Flask belongs to applications, and uWSGI and wsgiref belong to server gateways. Personal feeling, WSG

See all articles