CakePHP Auth允许JSON扩展

时间:2011-02-01 18:55:24

标签: php authentication cakephp cakephp-1.3

基本上,我想知道的是你是否可以使用Auth组件来允许某些扩展(JSON / HTML)?

基本上,假设我们有一个动作,动作就是索引。在此操作中,我们所做的只是列表作者(用户)。所以网址是http://somewebsite.com/authors/index。如果我们转到该URL,则内容类型将是HTML,应限制为登录用户(管理员),以便他们可以拥有编辑/删除按钮。但是,当你将.json扩展名放在它的末尾时,我们也会使用此操作来呈现json,因此url将为http://somewebsite.com/authors/index.json。在这种情况下,您不需要登录,因为您只想访问该信息。

那么,Auth组件是否可以允许某些扩展,这是最好的方法吗?

谢谢,干杯!

1 个答案:

答案 0 :(得分:3)

这些方面的某些内容应该有效(包括明确“仅解锁”特定方法):

public function beforeFilter() {
    $methods = array('index', 'foo', 'bar');

    // please forgive the terrible indentation
    if (in_array($this->action, $methods) &&
        isset($this->params['ext']) && $this->params['ext'] == 'json'
    ) {
        $this->Auth->allow($this->action);
    }
}
相关问题