测试环境,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']获取值