Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查控制器CakePhp 3中设置为允许的页面?_Php_Cakephp 3.0_Cakephp 3.1 - Fatal编程技术网

如何检查控制器CakePhp 3中设置为允许的页面?

如何检查控制器CakePhp 3中设置为允许的页面?,php,cakephp-3.0,cakephp-3.1,Php,Cakephp 3.0,Cakephp 3.1,我有一个注销功能,我正在尝试创建,将检查当前页面是否允许注销用户。如果是这样,我将留在页面上,如果不是,我将重定向到主页。我想知道如何检查当前页面是否被允许。我可以检查他们是否有此代码的授权: public function logout() { if($this->isAuthorized($this->Auth->user())) { $this->Auth->logout(); $redirect = $this->

我有一个注销功能,我正在尝试创建,将检查当前页面是否允许注销用户。如果是这样,我将留在页面上,如果不是,我将重定向到主页。我想知道如何检查当前页面是否被允许。我可以检查他们是否有此代码的授权:

public function logout()
{
    if($this->isAuthorized($this->Auth->user())) {
        $this->Auth->logout();
        $redirect = $this->redirect($this->referer());
    } else {
        $this->Auth->logout();
        $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
    }
    return $redirect;
}
但我无法检查是否允许使用当前页面:

public function logout()
{
    if(in_array($this->request->here, $this->Auth->allow())) {
        $this->Auth->logout();
        $redirect = $this->redirect($this->referer());
    } else {
        $this->Auth->logout();
        $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
    }
    return $redirect;
}

我想出了一个答案,但如果有更好的方法,我还是想知道。我将隐藏输入中的操作传递给控制器方法,并检查allowedPages数组中是否有

public $allowedPages = [];
public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->allowedPages = ['checkout', 'charge', 'logout'];
    $this->Auth->allow($this->allowedPages);
}

public function logout()
{
    if(in_array($this->request->data('action'), $this->allowedPages)) {
        $this->Auth->logout();
        $redirect = $this->redirect($this->referer());
    } else {
        $this->Auth->logout();
        $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
    }
    return $redirect;
}

最佳实践是使用
$this->Auth->allowedActions
,而Auth组件将允许的操作存储在属性中。 您可以按以下方式调用它们:

$actions = $this->Auth->allowedActions;