简体   繁体   English

在cakephp 3.0中单击提交按钮后无法重定向到下一页

[英]Cannot redirect to next page on clicking of a submit button in cakephp 3.0

I'm new to cakephp i have a form which is a login form on clicking of a login form submit button the page does'nt redirect. 我是Cakephp的新手,我有一个表单,该表单是单击登录表单提交按钮后页面不会重定向的登录表单。 It returns to the same page ie Login page. 它返回到同一页面,即登录页面。 The problem is removed when i remove LoginAction from AppController. 当我从AppController删除LoginAction时,该问题已消除。 My code is as follows: 我的代码如下:

index.ctp index.ctp

<?php echo $this->Form->create('Login', array('url' => array('controller' => 'Login', 'action' => 'dashboard'))); ?>
<?= $this->Form->input('username'); ?>
<?= $this->Form->input('password'); ?>
<?= $this->Form->submit('Login', array('class' => 'button')); ?>
<?= $this->Form->end(); ?> 

AppController.php AppController.php

 $this->loadComponent('Auth',['loginAction' => [
                        'controller' => 'Login',
                        'action' => 'index'
                     ],'authenticate' => [
                          'Form' => [
                           'userModel' => 'Login', // Added This
                            'fields' => [
                              'username' => 'username',
                              'password' => 'password',
                             ]
                           ]
                     ],'loginRedirect' => [
                         'controller' => 'Post',
                         'action' => 'dashboard'
                     ],
                ]);

LoginController.php LoginController.php

public function dashboard()
{
    echo "sample msg";
    $this->render('dashboard');
}


  public function login(){
   if($this->request->is('post')){
     $user=$this->Auth->identify();
     if($user) {
     $this->Auth->setUser($user);
     return $this->redirect(['controller'=>'Post']);
     }
     $this->Flash->error('Incorrect Login');
       }  
    }

If your problem is related with redirecting just add this in respective controller: 如果您的问题与重定向有关,只需将其添加到相应的控制器中:

public function initialize()
{
    parent::initialize();
    $this->Auth->allow(['dashboard']); // your actions list
}

You need to allow actions if you need to access them just before login get success. 如果需要在登录成功之前访问它们,则需要允许操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM