在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 新手。请有人帮助我。谢谢

您可以在 AppController.php

中添加 loginRedirect Auth 组件配置
$this->loadComponent('Auth', [
            'authorize' => ['Controller'], // Added this line
            'loginRedirect' => [
                'controller' =>Blogs',
                'action' => 'index'
            ]
]);

现在应该可以了。

您可以查看详细信息 Auth config

这是我的控制器

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() ?>