如何解决Cakehp 4.x在登录表单前重定向到上一页
我的登录表格:
<div class="users form">
<h3>Login</h3>
<?= $this->Form->create()?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<?= $this->Form->control('email',['required' => true]) ?>
<?= $this->Form->control('password',['required' => true]) ?>
<?= $this->Form->hidden('origin',['value' => $this->request->referer()]) ?>
</fieldset>
<?= $this->Form->submit(__('Login')); ?>
<?= $this->Form->end() ?>
<?= $this->Html->link(__("Register as New User"),['action' => 'register']) ?>
<?= $this->Html->link(__("Password reset"),['action' => 'recall']) ?>
我的用户控制器(登录方法) 控制器使用“ Cake \ Routing \ Router;”和“ Cake \ Http \ Response;”
public function login()
{
$this->request->allowMethod(['get','post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET,redirect if user is logged in
if ($result->isValid()) {
//from where did the user come to the login form
$origin=$this->request->getData('origin');
//define where to redirect,if $origin is not valid
$redirect = $this->request->getQuery('redirect',[
'controller' => 'Recipes','action' => 'index',]);
$origin=substr($origin,1);
$origin = Router::url('/',true).$origin;
$this->response = $this->response->withLocation($origin);
//debug($this->redirect($redirect));
//die();
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
我希望用户重定向到他们进入登录表单的页面。 上面的脚本做了我想要的,但是我不认为这是按照Cake约定的正确方法。 有人可以帮我做到这一点吗? 最好的祝福 德克
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。