如何从 OctoberCMS 插件中的事件处理程序重定向?
How to redirect from event handler in OctoberCMS plugin?
我已经在我的插件的启动事件中为 'rainlab.user.logout' 注册了一个侦听器。
public function boot()
{
Event::listen('rainlab.user.logout', function ($controller) {
return Redirect::to('https://www.google.com');
});
}
它什么都不做。我已使用
确认流量正在到达此块
header("Location: https://www.google.com");
exit;
代替 return 语句,该语句给出一个警告框说 "error" 没有重定向。
您可以使用注销 Ajax link 的数据属性重定向到您想要的任何地方。检查下面的例子。 [ 为此,您需要将会话组件添加到您的页面]
查看更多信息:https://octobercms.com/plugin/rainlab-user#documentation
<a data-request="onLogout"
data-request-data="redirect: 'https://www.google.com'"
>
Sign out
</a>
Still if you want to use event to redirect user you can use below code. Note : its kind of hacky approach not recommended but yes it works
.
Event::listen('rainlab.user.logout', function ($controller) {
$_POST['redirect'] = 'https://www.google.com';
});
Why your approach is not working [ Extra for understanding only ]
Because in event handler they did not catch any data which you return from event, you are returning Redirect
but in handler they don't catch it or receive it, this event rainlab.user.logout
just fired and let you access $user
object data nothing more. so your return statement has no effect on it.
内容 : plugins/rainlab/user/components/Session.php
public function onLogout()
{
$user = Auth::getUser();
Auth::logout();
if ($user) {
Event::fire('rainlab.user.logout', [$user]);
// ^ see, nothing is here which can accept your return redirect.
}
$url = post('redirect', Request::fullUrl());
Flash::success(Lang::get('rainlab.user::lang.session.logout'));
return Redirect::to($url);
}
如有疑问请评论。
我已经在我的插件的启动事件中为 'rainlab.user.logout' 注册了一个侦听器。
public function boot()
{
Event::listen('rainlab.user.logout', function ($controller) {
return Redirect::to('https://www.google.com');
});
}
它什么都不做。我已使用
确认流量正在到达此块header("Location: https://www.google.com");
exit;
代替 return 语句,该语句给出一个警告框说 "error" 没有重定向。
您可以使用注销 Ajax link 的数据属性重定向到您想要的任何地方。检查下面的例子。 [ 为此,您需要将会话组件添加到您的页面]
查看更多信息:https://octobercms.com/plugin/rainlab-user#documentation
<a data-request="onLogout"
data-request-data="redirect: 'https://www.google.com'"
>
Sign out
</a>
Still if you want to use event to redirect user you can use below code. Note : its kind of hacky approach
not recommended but yes it works
.
Event::listen('rainlab.user.logout', function ($controller) {
$_POST['redirect'] = 'https://www.google.com';
});
Why your approach is not working [ Extra for understanding only ]
Because in event handler they did not catch any data which you return from event, you are
returning Redirect
but in handler they don't catch it or receive it, this eventrainlab.user.logout
just fired and let you access$user
object data nothing more. so your return statement has no effect on it.
内容 : plugins/rainlab/user/components/Session.php
public function onLogout()
{
$user = Auth::getUser();
Auth::logout();
if ($user) {
Event::fire('rainlab.user.logout', [$user]);
// ^ see, nothing is here which can accept your return redirect.
}
$url = post('redirect', Request::fullUrl());
Flash::success(Lang::get('rainlab.user::lang.session.logout'));
return Redirect::to($url);
}
如有疑问请评论。