Search code examples
phpcakephpbefore-filter

CakePHP v3.0 beforeFilter correct syntax


I'm building a project using CakePHP v3.0. This is my original beforeFilter method inside a controller:

public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow('add','logout','authorize');
    }

This worked until I installed the app on a different server and 'authorize' action stopped being accessible without authentication. The only solution I found was putting the actions inside an array, like this:

public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow(array('add','logout','authorize'));
    }

If I didn't misunderstood CakePHP 3 book the first case should work, shouldn't it?. What's the correct syntax for this situation?. Thanks in advance.


Solution

  • I kinda doubt that the first variant really worked, it was probably just luck, respectively another problem that made it possible to access your guarded authorize() action, as the AuthComponent::allow() method takes only a single argument, which is either the name of a single action, or an array of actions.

    So, long story short, multiple actions must be passed as arrays.

    See also