登录页面不会转到下一页; cakephp 3.4

时间:2017-06-15 21:06:52

标签: php cakephp-3.0

我已尝试过所有内容,我的登录页面始终保留在登录页面....

这是我的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 -->

1 个答案:

答案 0 :(得分: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'     
          ]
    ]);

我评论了新的变化