成功登录后,我想被重定向到我登录前所在的屏幕。get LoginRedirect() 不起作用
After a successful login, I want to be redirected to the screen I was on before I logged in. getLoginRedirect() does not work
假设您在用户列表屏幕中。从那里您将转到登录屏幕进行登录。在那里,您将输入您的电子邮件和密码并输入提交按钮。
您已成功登录。
如果登录成功,我们希望被重定向到用户列表屏幕。
如果您在用户详细信息屏幕中,您将被重定向到用户详细信息屏幕
如果用户在编辑屏幕上,则用户编辑屏幕
我已阅读 book.cakephp.org 上的 AuthenticationPlugin 文档。
在那里我学会了使用getLoginRedirect()
来实现这个功能。
我知道一旦我按照以下步骤进行设置,我想做的事情就会发生。
但是,getLoginRedirect() returns 为空。
我需要做什么?
我错过了什么?
怎么了?
// in Application.php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$path = $request->getPath();
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/',
'queryParam' => 'redirect', // <- I believe this is the only one that matters.
]);
// Abbreviated below....
}
// in UsersController
public function login()
{
$this->request->allowMethod(['get', 'post']);
if ($this->request->is('post')) {
$result = $this->Authentication->getResult();
$requestData = $this->request->getData();
if ($result->isValid()) {
// I want to get the original url. But null is passed.
$redirect = $this->Authentication->getLoginRedirect() ?? '/';
return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('メールアドレス、またはパスワードが間違っています。'));
}
}
}
我想我已经公开了与 getLoginRedirect() 相关的所有内容。如果有什么遗漏或者有什么想知道的,请随时告诉我。
请帮助我。请帮助我...
插件提供的登录重定向功能仅在您访问需要身份验证但未登录的 URL 后被重定向到登录 URL 时自动工作。在这种情况下身份验证中间件将使用当前 URL 设置 redirect
查询字符串变量,以便组件可以在重定向到登录 URL.
后获取它
如果您手动访问登录 URL,那么您还需要手动设置 redirect
查询字符串变量,即在您构建 link 的菜单中将您带到登录名,将当前的 URL 添加到查询字符串中,大致如下:
$service = $this->request->getAttribute('authentication');
// here `$queryParam` would by default be `redirect`
$queryParam = $service->getConfig('queryParam');
echo $this->Html->link('Login', [
'plugin' => null,
'prefix' => false,
'controller' => 'Users',
'action' => 'login',
'?' => [
$queryParam => $this->request->getRequestTarget(),
],
]);
因此,如果您使用 /users/show
,登录名 link 的 URL 将类似于:
/login?redirect=/users/show
并且构建登录表单的表单助手应该准确地选择 URL,以便在提交表单后,身份验证组件可以从当前 [=34] 读取重定向 URL =]相应地。
另见
假设您在用户列表屏幕中。从那里您将转到登录屏幕进行登录。在那里,您将输入您的电子邮件和密码并输入提交按钮。 您已成功登录。 如果登录成功,我们希望被重定向到用户列表屏幕。
如果您在用户详细信息屏幕中,您将被重定向到用户详细信息屏幕 如果用户在编辑屏幕上,则用户编辑屏幕
我已阅读 book.cakephp.org 上的 AuthenticationPlugin 文档。
在那里我学会了使用getLoginRedirect()
来实现这个功能。
我知道一旦我按照以下步骤进行设置,我想做的事情就会发生。
但是,getLoginRedirect() returns 为空。
我需要做什么? 我错过了什么? 怎么了?
// in Application.php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$path = $request->getPath();
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/',
'queryParam' => 'redirect', // <- I believe this is the only one that matters.
]);
// Abbreviated below....
}
// in UsersController
public function login()
{
$this->request->allowMethod(['get', 'post']);
if ($this->request->is('post')) {
$result = $this->Authentication->getResult();
$requestData = $this->request->getData();
if ($result->isValid()) {
// I want to get the original url. But null is passed.
$redirect = $this->Authentication->getLoginRedirect() ?? '/';
return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('メールアドレス、またはパスワードが間違っています。'));
}
}
}
我想我已经公开了与 getLoginRedirect() 相关的所有内容。如果有什么遗漏或者有什么想知道的,请随时告诉我。
请帮助我。请帮助我...
插件提供的登录重定向功能仅在您访问需要身份验证但未登录的 URL 后被重定向到登录 URL 时自动工作。在这种情况下身份验证中间件将使用当前 URL 设置 redirect
查询字符串变量,以便组件可以在重定向到登录 URL.
如果您手动访问登录 URL,那么您还需要手动设置 redirect
查询字符串变量,即在您构建 link 的菜单中将您带到登录名,将当前的 URL 添加到查询字符串中,大致如下:
$service = $this->request->getAttribute('authentication');
// here `$queryParam` would by default be `redirect`
$queryParam = $service->getConfig('queryParam');
echo $this->Html->link('Login', [
'plugin' => null,
'prefix' => false,
'controller' => 'Users',
'action' => 'login',
'?' => [
$queryParam => $this->request->getRequestTarget(),
],
]);
因此,如果您使用 /users/show
,登录名 link 的 URL 将类似于:
/login?redirect=/users/show
并且构建登录表单的表单助手应该准确地选择 URL,以便在提交表单后,身份验证组件可以从当前 [=34] 读取重定向 URL =]相应地。
另见