如何检查控制器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;