CakePHP Auth Allow JSON Extension

Essentially, I would like to know if the Auth component can be used to allow certain extensions (JSON / HTML)?

Basically, let's say we have one action, the action is an index. In this action, all we do is list authors (Users). So the URL is http://somewebsite.com/authors/index . If we go to this URL, the type of content will be HTML, which should be limited to registering users (administrators) so that they can have edit / delete buttons. However, we also use this action to represent json when you add the .json extension to the end, so the url will be http://somewebsite.com/authors/index.json.In this case, you will not need to register because you just want to access this information.

So, is it possible for the Auth component to allow certain extensions, and is this the best way to do this?

Thanks and greetings!

+3
source share
1 answer

Something in this direction should work (including explicitly “unlocking” only certain 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);
    }
}
+3
source

Source: https://habr.com/ru/post/1789318/


All Articles