In my LoginController I have defined a custom redirect route like this:
protected $redirectTo = '/home';
which works fine but I want to show a toastr notification when user logs in therefore I want to flash some data with this redirect.
In my other controllers I'm doing something like this and it works good
$notification = array(
'message' => 'User okay',
'alert_type' => 'success',
);
return Redirect::route('user')->with('notification', $notification);
but I can't figure out how to it with the $redirectTo
Please help me with this
One way to pass a "message bag", is to create a redirectTo method in the LoginController.
Out of the box this method is not present, because it is inherited from the trait RedirectsUsers so, just wrote it and complete as needed and it will be executed just when the user had been authenticated (as the function name say)
protected function redirectTo(Request $request)
{
$notification = array(
'message' => 'User okay',
'alert_type' => 'success',
);
return Redirect::route($this->redirectTo)->with('notification', $notification);
}
As you can see, the trait RedirectsUsers is looking for a defined redirectTo function or using the variable you define as $redirectTo
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
Otherwise, if the parameters you would like to pass is a simple GET parameter, you can use the same authenticated function and set it with the parameters as you prefer, also dynamic params like
protected function redirectTo(Request $request)
{
return redirect()->route('route.name', [param1 => value1, param2 => value2]);
}
For the redirect purpose this is more clear then using other functions like the "authenticated" ones suggested before.
you can set a method redirectTo() with your custom logic in your LoginController. That method will be called first in redirectPath() from trait RedirectsUsers.
hope it helps CV
simply you can do with get params, /home?your_data='blabla'&another_data='blabla'
and after that use that params in controller to show in view.
But in laravel you can do this more pretty;
Route('home{notification?}','YourController@yourAction');
and in YourController you shoud do
public function yourAction($notification = null) {
if(isset($notification) {
//do something with notification
}
}