当我尝试使用$ this-> Auth-> user()时它可以工作,但是当我添加id $ this-> Auth-> user(' id')时它不会工作一片空白

时间:2016-04-14 23:13:23

标签: authentication cakephp login

$ this-> Auth-> user(' id')始终为null appcontrollers代码:

<?php
 App::uses('AuthComponent', 'Controller/Component');
 App::uses('Controller', 'Controller');
 class AppController extends Controller {
//public $components = array('DebugKit.Toolbar','Session');
  public $components = array(
    'Acl',
    'Flash',
    'Auth' => array(
        'authorize' => array(
            'Actions' => 
       array('actionPath' => 'controllers','action'=>'index')
        )
    ),
    'Session'
);
public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->allow();
    $this->Auth->loginAction = array(
      'controller' => 'personnes',
      'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
      'controller' => 'personnes',
      'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
      'controller' => 'biens',
      'action' => 'index'
    ); }}

Personnescontrollers

public function beforeFilter() {
parent::beforeFilter();

$this->Auth->allow();
 }

public function login() {
if ($this->request->is('post')) {
     $user = $this->Personne->findAllByLoginAndMdp
  ($this->data['Personne']['login'],$this->data['Personne']['mdp']);

    if ($this->Auth->login($user)) {
   return $this->redirect($this->Auth->redirectUrl()); 

     } else {
        $this->Session->setFlash(__
       ('Votre nom d\'user ou mot de passe sont incorrects.'));
       }
     }
     }

bienscONTROLLERS

     public function index( ) {
     $this->Bien->recursive = 0;
      $this->paginate = array(
      'paramType' => 'querystring',
    'limit' => 5
        );
      $biens = $this->paginate('Bien');
      $this->set(compact('biens'));
    $user = $this->Auth->user('id');
    Debugger::dump($user);
     }

Personne model`

<?php
    App::uses('AuthComponent', 'Controller/Component');
     App::uses('AppModel', 'Model');
     class Personne extends AppModel {`
      public function beforeSave($options = array()) {
      $this->data['Personne']['mdp'] = 
      AuthComponent::password($this>data['Personne']['mdp']);
       return true;
       }

当我尝试$ this-&gt; Auth-&gt; user()时它可以工作,但是当我添加id $ this-&gt; Auth-&gt; user()&#39; id&#39;它不起作用 Plz我该怎么办?有人能帮助我吗?

0 个答案:

没有答案