开发者

CakePHP Auth Allow JSON Extension

Essentially, what I would like to know is if you can use the Auth Component to allow certain extensions (JSON/HTML)?

Basically, lets say we have one action, the action is index. In this action all we do is list authors (Users). So the url is http://somewebsite.com/authors/index. If we go to that url the content type would be HTML, w开发者_如何转开发hich should be restricted to logged in users (Admins) so that they can have Edit/Delete buttons. However, we also use this action to present json when you put the .json extension at the end of it, so the url will be http://somewebsite.com/authors/index.json. In this case, you wouldn't need to be logged in because you just want to access that information.

So, is it possible for the Auth Component to allow certain extensions, and is this the best way to go about it?

Thanks and Cheers!


Something along these lines should work (including explicitly "unlocking" only specific methods):

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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜