php – 使用Yii重定向访问规则
我正在做一个需要身份验证的应用程序.在应用程序的索引页面中,我指定了这样的访问规则
public function accessRules() { return array( array('deny','actions'=>array('index','register','login','password'),'users'=>array('@'),),array('allow','users'=>array('*') ),); } 在第一条规则中,认证用户无法访问动作“索引”,“注册”,“登录名”和“密码”.但是,我不想显示此消息 Unauthorized You are not authorized to perform this action. You do not have the proper credential to access this page. If you think this is a server error,please contact the webmaster. …验证用户尝试访问这些操作时.相反,我想将它们重定向到另一个页面.如果我可以在第一条规则下做这样的事情,这将是有用的 array('redirect','url'=>array('home/index'),
他们会让你不能拒绝的报价
从Yii v1.1.11开始CAccessRule定义了 原来的答案如下. 选项1:扩展Yii启用此功能(正确) 为了做到这一点,我们需要编写自己的类来代替 class MyAccessRule extends CAccessRule { public $redirect; // just add this property } 对于CAccessControlFilter,我们要让它识别这个属性的值,然后对它进行处理.为此,我们需要覆盖 class MyAccessControlFilter extends CAccessControlFilter { protected function preFilter($filterChain) { $app=Yii::app(); $request=$app->getRequest(); $user=$app->getUser(); $verb=$request->getRequestType(); $ip=$request->getUserHostAddress(); foreach($this->getRules() as $rule) { if(($allow=$rule->isUserAllowed($user,$filterChain->controller,$filterChain->action,$ip,$verb))>0) // allowed break; else if($allow<0) // denied { // CODE CHANGED HERE $request->redirect($app->createUrl($rule->redirect)); return false; } } return true; } } 然后我们还需要覆盖 $r=new CAccessRule; 读书 $r=new MyAccessRule; 在创建这些课程之后,我们也必须将它们注入到Yii的管道中.为此,在基本控制器类上覆盖 public function filterAccessControl($filterChain) { $filter=new MyAccessControlFilter; // CHANGED THIS $filter->setRules($this->accessRules()); $filter->filter($filterChain); } 而已!现在,您可以通过向访问控制过滤器提供新的重定向参数,从而在任何控制器中利用额外的功能: public function accessRules() { return array( array('deny','redirect'=>array('home/index'),); } 选项2:在每个动作内部实现访问控制(要避免) 如果您对Yii的核心组件进行子类化并不舒服,我不建议的另一个选项是将要保护的每个控制器操作中嵌入访问控制和重定向逻辑,或者覆盖控制器上的 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |