繁体   English   中英

Laravel - LoginController | 如何使用参数设置$ redirectTo路由?

[英]Laravel - LoginController | How to set $redirectTo route with parameters?

在我的LoginController我已经定义了一个自定义重定向路由,如下所示:

protected $redirectTo = '/home';

工作正常,但我想在用户登录时显示toastr通知,因此我想使用此重定向flash一些数据。

在我的其他控制器中,我正在做这样的事情并且效果很好

$notification = array(
                        'message' => 'User okay', 
                        'alert_type' => 'success',
                      );
return Redirect::route('user')->with('notification', $notification);

但我无法弄清楚如何使用$redirectTo

请在这件事上给予我帮助

传递“消息包”的一种redirectTo method是在LoginController中创建redirectTo method

开箱即用这个方法不存在,因为它是从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 function或使用您定义为$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]);
}

对于重定向目的,这更清楚,然后使用其他功能,如之前建议的“已验证”。

您可以在LoginController中使用自定义逻辑设置方法redirectTo()。 该方法将首先在trairect RedirectsUsers的redirectPath()中调用。

希望它有助于简历

只需你可以使用/home?your_data='blabla'&another_data='blabla' params, /home?your_data='blabla'&another_data='blabla'然后在控制器中使用params来显示在视图中。 但是在laravel你可以做得更漂亮;

Route('home{notification?}','YourController@yourAction');

你应该在你的控制器中做

public function yourAction($notification = null) {
   if(isset($notification) {
//do something with notification
}
}

您可以通过在LoginController中编写此函数来执行此操作“

protected function authenticated(Request $request, $user)
{
    $notification = array(
                    'message' => 'User okay', 
                    'alert_type' => 'success',
                  );



return Redirect::route('user')->with('notification', $notification);
}

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM