CakePHP:调用成员函数allow()
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(). 只是推测:如果未经授权的人试图访问受保护的资源,默认的Auth组件会重定向到/ users / login.当您保护UsersController并且不允许登录操作时,这可能会导致您遇到的行为. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |