命令行方式運行yii2程序


測試環境,yii 2.0.3版本

web訪問方式,控制器放在controllers目錄下 ,瀏覽器訪問如下地址

http://127.0.0.1/index.php?r=[controller-name]/[method-name]

console訪問方式,控制器放在commands目錄下

./yii [command-name]/[method-name]

示例:

<?php

namespace app\commands;

use Yii;
use yii\console\Controller;
use app\models\Account;

class CronController extends Controller
{
    public function actionGetAccounts()
    {
        $accounts = Account::find()->all();
        if(is_array($accounts)){
                $data = $this->renderFile(dirname(__DIR__) . '/assets/account_template.php', ["accounts"=>$accounts], $this);
                echo $data;
        }else{
                exit(-1);
        }
    }
}

模板account_template.php如下

<?php
    foreach($accounts as $account){
        echo "account=".$account->name."\n";
    }
?>

附yii配置文件讀取方式

config/params.php
<?php

return [
    'key' => 'value',
    'key1' => 'value1'
];
controller,view,model使用時通過Yii::$app->params['key']獲取值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM