2017-06-15 58 views
0

我已經試過一切,我的登錄頁面總是停留在登錄頁面....登錄頁面不會進入下一頁; CakePHP的3.4

這是我appcontroller.php

public function initialize() 
{ 
    parent::initialize(); 
    $this->loadComponent('RequestHandler'); 
    $this->loadComponent('Flash'); 

    $this->loadComponent('Auth', [ 
     'authorize'=> 'Controller',//added this line 
     'authenticate' => [ 
      'Form' => [ 
       'fields' => [ 
        'username' => 'username', 
        'password' => 'password' 
       ] 
      ], 
      //'scope'=>['Users.activo' => 1] 
     ], 
     'loginAction' => [ 
      'controller' => 'Users', 
      'action' => 'login' 
     ], 
     'loginAction' => [ 
      'controller' => 'Users', 
      'action' => 'login' 
     ], 
     'unauthorizedRedirect' => $this->referer() // If unauthorized, return them to page they were just on 
    , 'loginRedirect' => [ 
     'controller' => 'Users', 
     'action' => 'index'  
    ] 
       ]); 


    $this->Auth->allow(['display','index','add']); 
    //$this->Auth->allow('login', 'logout'); 

    $this->LoadModel('Huespedesxhabitaciones'); 
    $arr = $this->Huespedesxhabitaciones->ExQuery('SELECT checkin, checkout, concat(huespedes.nombre, " ", huespedes.apellidos) as huesped, habitaciones.nombre as habitacion fROM huespedesxhabitaciones INNER JOIN huespedes ON huespede_id = huespedes.id INNER JOIN habitaciones ON huespedesxhabitaciones.habitacione_id = habitaciones.id WHERE estado_id = 1'); 
    $this->set('ocupado', $arr); 

    $this->LoadModel('Reservas'); 
    $arrs = $this->Reservas->ExQuery('SELECT reservas.id, reservas.habitacione_id, reservas.desde, reservas.hasta, reservas.precio, reservas.cliente as cliente, reservas.correo, habitaciones.nombre as habitacion FROM reservas INNER JOIN habitaciones ON reservas.habitacione_id = habitaciones.id'); 
    $this->set('reservado', $arrs); 

}` 

,這是我userscontroller.php

public function index() 
{ 
    $users = $this->paginate($this->Users); 

    $this->set(compact('users')); 
    $this->set('_serialize', ['users']); 
} 

/** 
* View method 
* 
* @param string|null $id User id. 
* @return \Cake\Network\Response|null 
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
*/ 
public function view($id = null) 
{ 
    $user = $this->Users->get($id, [ 
     'contain' => [] 
    ]); 

    $this->set('user', $user); 
    $this->set('_serialize', ['user']); 
} 

/** 
* Add method 
* 
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise. 
*/ 
public function add() 
{ 
    $user = $this->Users->newEntity(); 
    if ($this->request->is('post')) { 
     $user = $this->Users->patchEntity($user, $this->request->getData()); 
     if ($this->Users->save($user)) { 
      $this->Flash->success(__('The user has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('The user could not be saved. Please, try again.')); 
    } 
    $this->set(compact('user')); 
    $this->set('_serialize', ['user']); 
} 

/** 
* Edit method 
* 
* @param string|null $id User id. 
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise. 
* @throws \Cake\Network\Exception\NotFoundException When record not found. 
*/ 
public function edit($id = null) 
{ 
    $user = $this->Users->get($id, [ 
     'contain' => [] 
    ]); 
    if ($this->request->is(['patch', 'post', 'put'])) { 
     $user = $this->Users->patchEntity($user, $this->request->getData()); 
     if ($this->Users->save($user)) { 
      $this->Flash->success(__('The user has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('The user could not be saved. Please, try again.')); 
    } 
    $this->set(compact('user')); 
    $this->set('_serialize', ['user']); 
} 

/** 
* Delete method 
* 
* @param string|null $id User id. 
* @return \Cake\Network\Response|null Redirects to index. 
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
*/ 
public function delete($id = null) 
{ 
    $this->request->allowMethod(['post', 'delete']); 
    $user = $this->Users->get($id); 
    if ($this->Users->delete($user)) { 
     $this->Flash->success(__('The user has been deleted.')); 
    } else { 
     $this->Flash->error(__('The user could not be deleted. Please, try again.')); 
    } 

    return $this->redirect(['action' => 'index']); 
} 

public function isAuthorized($user){ 
    //return $this->request->session()->read('Auth.User.role_id')=="6"; 
    return true; 
} 
public function login(){ 
$this->log($this->request->data, LOG_DEBUG); 
$this->viewBuilder()->setLayout('login'); 
if ($this->request->is('post')) { 
    $user = $this->Auth->identify(); 
    if ($user) 
    { 
     $this->Auth->setUser($user); 
     return $this->redirect(['controller'=>'users', 'action' => 'index']); 
    } 
    $this->Flash->error('Nombre de usuario y/o contraseña incorrecta');} 

,這是我login.ctp

<?php $this->HTML->css('login') ?> 

    <div class="container"> 

     <div class="users form"> 
<?= $this->Flash->render('auth') ?> 
<?= $this->Form->create() ?> 
    <fieldset> 
     <legend><?= __('Please enter your username and password') ?></legend> 
     <?= $this->Form->input('username') ?> 
     <?= $this->Form->input('password') ?> 
    </fieldset> 
<?= $this->Form->button(__('Login')); ?> 
<?= $this->Form->end() ?> 
</div> 
    </div><!-- /container --> 

回答

0
$this->loadComponent('Auth', [ 
     'authorize'=> 'Controller', 
     'authenticate' => [ 
      'Form' => [ 
       'fields' => [ 
        'username' => 'username', 
        'password' => 'password' 
       ] 
      ],    
     ], 
     'loginAction' => [ 
      'controller' => 'Users', // Replace Your controller and action where you want redirect after login action 
      'action' => 'login' 
     ], 
     'unauthorizedRedirect' => $this->referer() // If unauthorized, 
return them to page they were just on 
     'loginRedirect' => [ 
      'controller' => 'Users', 
      'action' => 'index'  
      ] 
    ]); 

我已評論新的chang