2012-09-21 2 views
0

Authentication 코어 라이브러리 에 대한 설명서를 읽었으며 사용 방법이 명확하지 않습니다. 그것은 많은 사전 지식을 가정하는 것 같습니다. 특히CakePHP 2.2를 사용하여 사용자를 인증하는 방법은 무엇입니까?

는 :

<?php 
class PeopleController extends AppController { 
    public $helpers = array('Html', 'Form'); 

    $this->Auth->authenticate = array(
     AuthComponent::ALL => array('userModel' => 'Member'), 
     'Form', 
     'Basic' 
    ); 

    public function index() { 

    } 
} 

그리고이 예외 화재 :

syntax error, unexpected '$this' (T_VARIABLE), expecting function (T_FUNCTION) Error: An Internal Error Has Occurred.

Stack Trace CORE\Cake\Error\ErrorHandler.php line 162 → ErrorHandler::handleFatalError(integer, string, string, integer) [internal function] → ErrorHandler::handleError(integer, string, string, integer, array) CORE\Cake\Core\App.php line 926 → call_user_func(string, integer, string, string, integer, array) CORE\Cake\Core\App.php line 899 → App::_checkFatalError() [internal function] → App::shutdown()

아무도의 간단한 예제를 제공 할 수

You configure authentication handlers using $this->Auth->authenticate. You can configure one or many handlers for authentication. Using multiple handlers allows you to support different ways of logging users in. When logging users in, authentication handlers are checked in the order they are declared. Once one handler is able to identify the user, no other handlers will be checked. Conversely you can halt all authentication by throwing an exception. You will need to catch any thrown exceptions, and handle them as needed.

You can configure authentication handlers in your controller’s beforeFilter or, in the $components array. You can pass configuration information into each authentication object, using an array:

그래서 내 PeopleController에서 나는 예제 코드에 쓴 인증 된 사용자 만 액세스 할 수 있도록 컨트롤러를 보호하는 방법? 개별 행동 기능을 보호 할 수 있습니까?

답변

-1

내가 잘못 아니에요 경우에 당신은 마틴이 이미 언급 한 바와 같이 $this 현재를 의미로, $this는 (Object) 메소드에서만 사용할 수 있습니다, 또한 beforeFilter()

public function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Auth->authenticate = array(
     AuthComponent::ALL => array('userModel' => 'Member'), 
     'Form', 
     'Basic' 
    ); 
} 

그리고 그것을해야 메서드가 호출 된 객체입니다.

0

클래스 정의 중에 인스턴스 참조를 가질 수 없습니다.

은 클래스 메소드에서만 액세스 할 수 있습니다. 컨트롤러에서

(말 AppController.php하자) :

public $components = array(
    'Auth' => array(
     'loginAction' => 'login', 
     'loginRedirect' => 'home', 
     'authenticate' => array(
      'Form' => array(
       'userModel' => 'User', 
       'fields' => array('password' => 'password'), 
       'scope' => array('User.active' => true) 
      ) 
     ) 
    ) 
);