在我的 LoginController
中,我定义了一个自定义重定向路由,如下所示:
protected $redirectTo = '/home';
工作正常,但我想在用户登录时显示 toastr 通知,因此我想通过此重定向flash
一些数据。
在我的其他 Controller 中,我正在做类似的事情并且效果很好
$notification = array(
'message' => 'User okay',
'alert_type' => 'success',
);
return Redirect::route('user')->with('notification', $notification);
但我不知道如何使用 $redirectTo
请帮我解决这个问题
最佳答案
传递“消息包”的一种方法是在 LoginController 中创建一个 redirectTo 方法
。
开箱即用此方法不存在,因为它继承自 trait RedirectsUsers
因此,只需编写它并根据需要完成即可执行当用户通过身份验证时(如函数名称所示)
protected function redirectTo(Request $request)
{
$notification = array(
'message' => 'User okay',
'alert_type' => 'success',
);
return Redirect::route($this->redirectTo)->with('notification', $notification);
}
如您所见,trait RedirectsUsers
正在寻找已定义的 redirectTo 函数
或使用您定义为 $ 的变量重定向至
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
否则,如果您要传递的参数是简单的 GET 参数,您可以使用相同的身份验证函数并使用您喜欢的参数进行设置,也可以使用动态参数,例如
protected function redirectTo(Request $request)
{
return redirect()->route('route.name', [param1 => value1, param2 => value2]);
}
出于重定向目的,这比使用其他功能(例如之前建议的“经过身份验证”的功能)更清晰。
关于php - Laravel - 登录 Controller |如何带参数设置$redirectTo路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680523/