2012-10-19 21 views
4

ProtectedController.phpCakePHP的:調用一個成員函數允許()

<?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'), 
     'logoutRedirect' => array('controller' => 'home', 'action' => 'index') 
    ) 
); 

到AppController然後覆蓋,然後允許所有人在那裏:

class AppController extends Controller { 

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

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

您將需要在UserController中添加$組件 –

+0

@MoyedAnsari我似乎並不需要它在UserController中。 –

+0

確實增加了函數__construct(){parent :: __ construct(); }在你的受保護的控制器中修復它? – Steve

回答

1

我正在做類似的事情。您控制器的主要區別是:

  • AppController包括Auth組件的歡迎,並不會在其beforeFilter一個$this->Auth->allow()
  • 我的ProtectedController沒有聲明login/logoutRedirects,並且調用其父母的beforeFilter
  • 我的UsersController根本沒有擴展ProtectedController。我受保護的控制器只包含Auth組件,並呼叫其父母的beforeFilter

只是猜測:如果未經授權的人試圖訪問受保護的資源,默認的Auth組件將重定向到/ users/login。由於您保護的UsersController和而不是允許login操作,也許這會導致您遇到的行爲。

相關問題