1在frontend/libs新建TestAction.php
<?php namespace frontend\libs; use yii\base\Action; class TestAction extends Action { public $param1=NULL; public $param2=NULL; public function run($get=NULL) { return $this->controller->render('test',[ 'get'=>$get, 'param1'=>$this->param1, 'param2'=>$this->param2 ]); } }
2在SiteController里调用testAction:
public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], 'test'=>[ 'class'=>'frontend\libs\TestAction', 'param1'=>'参数一', 'param2'=>'参数二', ] ]; }
3配置视图views/site/test.php完成测试:
<h1>TestAction</h1> <p>这是TestAction演示页面!</p> <p>$get="<?=$get?>"</p> <p>$param1="<?=$param1?>"</p> <p>$param2="<?=$param2?>"</p>
http://127.0.0.8/index.php?r=site%2Ftest&get=xxx
$get、$param1、$param2,其中$get是在url中传递的,例如按照我电脑上的配置,访问http://127.0.0.8/index.php?r=site%2Ftest&get=xxx,就会为$get赋值xxx。而$param1和$param2则是在controller中设置的。