1. 程式人生 > >yii2框架-console的cli模式(二十)

yii2框架-console的cli模式(二十)

又有兩週的時間沒有寫blog了,沒辦法最近實在是太忙了,公司的專案一直在趕,而且還要使用nodejs去實現功能,所以整個團隊的又得乖乖認真的學習nodejs。
網際網路技術每天都在日新月異的快速發展,我們作為一名設計與開發人員,每天都要關注整個移動網際網路的發展趨勢,掌握實時動態。
好吧,迴歸正題,yii2作為一個高效能的web開發框架,在處理後臺任務方面有什麼優勢?是否可以實現cli的模式實現框架本身的後臺任務?
答案是可以的!
在yii2的框架中,有一個console(控制檯模組)類,可以實現很多的yii2中的命令列功能。
大家可以http://www.yiichina.com/doc/guide/2.0/tutorial-console看看具體內容

下面介紹的主要是yii2的cli的模式執行後臺的任務處理。
(1)console的入口檔案

在base版本中,有一個yii的檔案,這個檔案的程式碼如下:

#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/config/console.php');

$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);
其實這是一個php的shell指令碼(cli模式指令碼),可以看到,入口檔案和web應用的入口檔案沒有多大的區別的。

(2)配置檔案
在console資料夾下有一個console.php檔案,這個檔案就是配置console需要應用到的元件
其中一項
'controllerNamespace' => 'app\commands',

表示console的邏輯業務(Controller)將在@app/commands的這個資料夾下面,預設一個controller
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\commands;

use yii\console\Controller;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <[email protected]>
 * @since 2.0
 */
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";
    }
}
在這裡我們可以實現很多的控制器的方法操作。

那麼在控制檯中我們可以直接呼叫命令執行這個對應的操作
首先進入值basic的資料夾下,然後
yii hello/index 
可以直接執行。

這個是在basic的版本上比較容易實現,那麼在advanced高階版本如何實現?
在advanced中,分為common公共模組,frontend前臺應用,backend後臺應用,console控制檯應用
(1)console的入口檔案
advanced版本中需要一個console的入口文,我們可以在console資料夾下面建立一個yii檔案
#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

// 一定要包含這個檔案進來,完成設定的@console的別名
require(__DIR__ . '/../common/config/bootstrap.php');

$config = require(__DIR__ . '/config/main.php');

$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);
注意這個require(__DIR__ . '/../common/config/bootstrap.php');
因為boostrap檔案裡面是設定各個模組應用的別名的。所以在進入這個模組或者應用時,必須要設定設定對應模組的別名,否則沒法識別,無法載入檔案。

(2)配置檔案
在console資料夾下有一個config資料夾,可以配置裡面的各個檔案,現在主要說一下的是main.php的主配置檔案,其中一項
'controllerNamespace' => 'console\controllers',

這個要配置好,在控制器中可以使用該配置的名稱空間,那麼可以在console/controllers下建立controller
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace console\controllers;

use yii\console\Controller;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <[email protected]>
 * @since 2.0
 */
class TestController 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";
    }

    public function actionCreate() {
    	echo 'advanced';
    }
}
具體的業務就可以在這裡實現
在終端控制檯進入console的資料夾下,執行yii test/index
可以列印hello word。

通過上面的basic版本和advanced版本的,可以利用定時任務來做一些後臺的業務處理。

友情推薦

本人已開源基於swoole擴充套件實現的輕量級框架

https://github.com/bingcool/swoolefy