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

Redirect 如何在cakephp 3.0中从该页面登录后重定向到同一页面,redirect,cakephp-3.0,Redirect,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 = $

这是我在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'));
 }
$this->loadComponent('Auth', [
            'authorize' => ['Controller'], // Added this line
            'loginRedirect' => [
                'controller' =>Blogs',
                'action' => 'index'
            ]
]);
在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中添加LoginDirect Auth组件配置

 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'));
 }
$this->loadComponent('Auth', [
            'authorize' => ['Controller'], // Added this line
            'loginRedirect' => [
                'controller' =>Blogs',
                'action' => 'index'
            ]
]);
现在应该可以了

有关详细信息,请查看 这是我的控制器

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


你好,alimon karim谢谢你的回答。然而,我正在寻找的是,我想重定向到url从我登录的地方,我也找到了方法,我已经张贴你的问题应该是“如何重定向最后一个url”,但祝贺你已经解决了它。