您的位置:首页 > 编程语言 > PHP开发

命令行方式运行yii2程序

2015-06-04 20:44 561 查看
测试环境,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']获取值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: