加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

CakePHP:调用成员函数allow()

发布时间:2020-12-13 22:45:46 所属栏目:PHP教程 来源:网络整理
导读:ProtectedController.php ?phpclass ProtectedController extends AppController { public $components = array( 'Session','Auth' = array( 'loginRedirect' = array('controller' = 'home','action' = 'index'),'logoutRedirect' = array('controller' = '
ProtectedController.php

<?php

class ProtectedController extends AppController {

    public $components = array(
        'Session','Auth' => array(
            'loginRedirect' => array('controller' => 'home','action' => 'index'),'logoutRedirect' => array('controller' => 'home','action' => 'index')
        )
    );

    public $paginate = array(
        'limit' => 2
    );

    public function beforeFilter() {
        $this->Auth->allow('index','view');
    }
}

AppController的

App::uses('Controller','Controller');


class AppController extends Controller {


}

UsersController

<?php

App::uses('ProtectedController','Controller');

class UsersController extends ProtectedController {

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

我一直在

Fatal Error
Error: Call to a member function allow() on a non-object    
File: /Library/WebServer/Documents/cakephp_stats/app/Controller/ProtectedController.php 
Line: 18

Notice: If you want to customize this error message,create app/View/Errors/fatal_error.ctp

现在的错误.

有人如何解决这个问题.从我看到它应该加载ProtectedController组件和AuthComponent将被加载.

编辑:

第18行是ProtectedController的这个:

public function beforeFilter() {
    $this->Auth->allow('index','view');
}

编辑:

我现在唯一能做的就是削减这个:

public $components = array(
    'Session','Auth' => array(
        'loginRedirect' => array('controller' => 'home','action' => 'index')
    )
);

到AppController然后覆盖然后允许每个人:

class AppController extends Controller {

    public $components = array(
        'Session','action' => 'index')
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('*');
    }
}

解决方法

我做了类似的事情.与控制器的主要区别是:

>我的AppController也包含Auth组件,并在其beforeFilter中执行$this-> Auth-> allow().
>我的ProtectedController没有声明login / logoutRedirects,并调用其父级的beforeFilter.
>我的UsersController根本不扩展ProtectedController.我的受保护控制器只包含Auth组件并调用其父级的beforeFilter.

只是推测:如果未经授权的人试图访问受保护的资源,默认的Auth组件会重定向到/ users / login.当您保护UsersController并且不允许登录操作时,这可能会导致您遇到的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读