繁体   English   中英

在cakephp 3.0中从该页面登录后如何重定向到同一页面

[英]how to redirect to the same page after loging in from that page in cakephp 3.0

这是我在UsersController.php中的登录方法

 public function login()
 {
    $user = $this->Users->newEntity();

    if ($this->request->is('post')) {

        $user = $this->Users->patchEntity($user, $this->request->data);
        $auth = $this->Auth->identify();
        if ($auth) {
            $this->Auth->setUser($auth);

            //return $this->redirect($this->Auth->redirectUrl());
            return $this->redirect(['controller' => 'Blogs', 'action' => 'index']);
        }

        $this->Flash->error(__('Invalid credentials.'));
    }
    $this->set(compact('user'));
 }

在AppController.php中

public function beforeFilter(Event $event)                                  
{
    /*if($this->here != '/users'){
        $this->Session->write('Auth.redirect', $this->here);
        } */

    $this->Auth->allow(['controller' => 'Blogs','action' => 'index', 'view']);

}

我是Cakephp的新手。 请有人帮我。 谢谢

这是我的控制器

public function login()
{
    //print_r($this->request->data);
    echo  $lasturl=Router::url( $this->here, true );                                //login from view page starts here  
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data);
        $auth = $this->Auth->identify();
        if ($auth) {
            $this->Auth->setUser($auth);

            $this->redirect($this->request->data['lasturl']);
            //return $this->redirect(['controller' => 'Blogs', 'action' => 'index']);
             //return     $this->redirect($this->request->session()->read($lasturl));
        }
    }                                                                           
    $this->set(compact('user'));
}

这是我的查看文件

<div class="login_comment">
<?= $this->Flash->render('auth') ?>
<?php echo $this->Form->create('Users', array('url' => array('controller' => 'Users', 'action' => 'login')));
?>
    <fieldset>
        <legend><?= __('Please Login to Comment for this Post') ?></legend>
        <?= $this->Form->input('username') ?>
        <?= $this->Form->input('password') ?>
        <?= $this->Form->input('lasturl',array('type'=>'hidden','value'=>$lasturl)) ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>

您可以在AppController.php中添加loginRedirect Auth组件配置

$this->loadComponent('Auth', [
            'authorize' => ['Controller'], // Added this line
            'loginRedirect' => [
                'controller' =>Blogs',
                'action' => 'index'
            ]
]);

现在应该可以工作了。

有关详细信息,您可以检查身份验证配置

暂无
暂无

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

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