yii2中独立动作actions的使用方法总结

在yii2的演示包里siteController.php文件中就有一段:

public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],//返回错误
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],//返回验证
];
}

在这段程序里,分别用到了error和captcha两个外部action,分别用来回显错误和校验图形验证码。
(我们可以在vendor/yiisoft/yii2里找到对应的程序文件。)
现在我们来看一下使用方法
在浏览器中我们输入http://localhost/index.php?r=site/captcha,则会显示出一个验证码;
在浏览器中我们输入http://localhost/index.php?r=site/error,则会显示出一个错误提示;
到这,相信大家应该对actions的使用方法有所了解了,actions就是一系列动作的聚合,以此来调用各个动作的使用。
现在我们来自己写一个独立动作

第一步:新建一个TestAction.php,自写义的外部action,可以放到任意位置,本例,我把它放到网站的frontend/actions里面。
TestAction.php的代码如下

controller->render('test');
}
}


第二步:在TestController中调用TestAction,代码如下

class TestController extends Controller{
public function actions()
{
return [
'test'=>[
'class'=>'frontend\libs\TestAction',
]
];
}


第三步:在views/test下添加test.php视图文件

独立动作测试

这是测试Actions的演示页面!

现在,我们就可以在初步浏览一下了
在浏览器中我们输入http://localhost/index.php?r=test/test,则会显示出刚才写的视图代码;

接下来再来点带参数的
第四步:修改TestAction。php

class TestAction extends Action {
public $param1=NULL;
public $param2=NULL;
public function run($param=NULL) {
return $this->controller->render('test',[
'param'=>$param,
'param1'=>$this->param1,
'param2'=>$this->param2
]);
}
}


可以看到有一个可通过浏览器传递的参数param,其他两个是可配置的参数。
第五步:修改view视图文件views/test/test.php:

TestAction

这是TestAction演示页面!

$param=""

$param1=""

$param2=""


在浏览器中输入http://localhost/index.php?r=test/test¶m=actiontest,则会显示出刚才修改的视图代码

好了独立动作的所有都进行了一遍,现象大家如果要修改默认提供的error效果,也可以找到相应的文件了。

最后,Yii2共提供了如下系统级的actions,可以直接使用:

InlineAction
CaptchaAction
CreateAction
DeleteAction
IndexAction
OptionsAction
UpdateAction
ViewAction
ErrorAction
GenerateAction

转:http://www.architecy.com/archives/361

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值