Php Laravel-LoginController |如何使用参数设置$RedirectToRoute?
在我的Php Laravel-LoginController |如何使用参数设置$RedirectToRoute?,php,laravel,laravel-5.4,Php,Laravel,Laravel 5.4,在我的LoginController中,我定义了如下自定义重定向路由: protected $redirectTo = '/home'; 这很好,但我想在用户登录时显示toastr通知,因此我想使用此重定向刷新一些数据 在我的其他控制器中,我正在做类似的事情,效果很好 $notification = array( 'message' => 'User okay', 'alert_type'
LoginController
中,我定义了如下自定义重定向路由:
protected $redirectTo = '/home';
这很好,但我想在用户登录时显示toastr通知,因此我想使用此重定向刷新一些数据
在我的其他控制器中,我正在做类似的事情,效果很好
$notification = array(
'message' => 'User okay',
'alert_type' => 'success',
);
return Redirect::route('user')->with('notification', $notification);
但我不知道如何使用$redirectTo
请帮我做这个您可以在LoginController中使用自定义逻辑设置redirectTo()方法。该方法将首先在trait redirectsuser的redirectPath()中调用 希望能有帮助
CV只需使用get params,
/home?您的_data='blabla'和另一个_data='blabla'
然后使用控制器中的参数在视图中显示。
但在拉威尔,你可以做得更漂亮
Route('home{notification?}','YourController@yourAction');
在你的控制器里,你应该这样做
public function yourAction($notification = null) {
if(isset($notification) {
//do something with notification
}
}
您可以通过在LoginController中编写此函数来完成此操作“ 希望有帮助。传递“消息包”的一种方法是在LoginController中创建一个
重定向到方法
开箱即用的方法不存在,因为它是从特性重定向器继承的,所以只需编写它并根据需要完成,它将在用户经过身份验证时执行(如函数名所示)
如您所见,特质重定向器
正在查找已定义的重定向到函数
或使用您定义为$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]);
}
出于重定向目的,这比使用之前建议的“已验证”功能更清楚。但是为什么我需要传递一个Request$Request
实例?它会给我一个错误,告诉我没有请求实例给定。我删除了请求参数并重试,但现在我得到了这个错误Route[/home]未定义。
我确信我的网站中有一条/home路径。php@sapnesNaik您有命名的路由吗?您可以使用返回路由('home',$arguments);
例如,在重定向到function@SapneshNaik您不必使用$request
对象。它应该以任何方式注入,但我可能错了。如果您没有命名的路由(如果将来您想在漂亮的URL上进行一些“重新设置样式”,则总是首选和舒适),您只需重定向(“/home”)
redirectTo方法需要一个路由('routeName'),而不需要重定向()->
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
protected function redirectTo(Request $request)
{
return redirect()->route('route.name', [param1 => value1, param2 => value2]);
}