php - 使用 Yii 重定向访问规则

标签 php yii

我正在做一个需要身份验证的应用程序。在应用程序的索引页面中,我指定了这样的访问规则

public function accessRules() {
    return array(
        array('deny',
            'actions'=>array('index','register','login','password'),
            'users'=>array('@'),
            ),
        array('allow',
                'users'=>array('*')
            ), 
        );
    }

在第一条规则中,'index'、'register'、'login' 和 'password' 操作对于经过身份验证的用户来说是不可访问的。但是,我不想显示此消息

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',
    'actions'=>array('index','register','login','password'),
    'users'=>array('@'),
    'url'=>array('home/index'),
    ),

最佳答案

从 Yii v1.1.11 开始,你可以用回调和闭包做同样的事情,而且只需要默认类:

array('deny',
'actions'=>array('index','register','login','password'),
'users'=>array('@'),
'deniedCallback' => function() { Yii::app()->controller->redirect(array ('/home/index')); }
),

关于php - 使用 Yii 重定向访问规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9804751/

相关文章:

php - 一旦表单的值进入PHP数据库,如何使表单消失

mysql - 使用yii框架在mysql中存储日期时间

php - 多个 MSQL 表和外键

php - Yii2 - 如何强制响应 JSON 格式化程序使用对象而不是数组?

php - 登录在 localhost 中工作,但登录在远程 yii 中不工作

php - 在 Yii 框架中实现 APNS

php - 如果目前实际上没有必要,是否愿意将大命令分解为较小的命令?

php - Codeigniter 3 显示所有在线用户

javascript - 来自 chrome 扩展的 MySql 连接

php - 有没有一种简单的方法可以创建一个包含 500000(虚拟/零/空)行的表