如何从控制器方法重定向到路由

2023-11-25

我在控制器中定义了一个方法,首先检索输入,如果我的数据库中存在电子邮件字段,我想返回一个视图。但是,如果电子邮件字段不存在,我想重定向到另一条路线。我也想将输入传递给该路线。

为了更好地理解我的意思,我的控制器代码如下:

    public function index(Request $request) {

    $credentials = $request->all();

    if (\App\User::where('email','=',$credentials['email'])->exists()){

        //if they are registered, return VIEW called welcome, with inputs

        return view('welcome', $credentials);

    }
    else{//If the user is not in the database, redirect to '/profile' route,
         //but also send their data

        return redirect('profile', $credentials);

    }

我的web.php如下:

Route::post('/profile', function() {
     $m = Request::only('email'); //I only need the email
     return view('profile', $m);
});

但是,此逻辑失败并出现错误:“未定义 HTTP 状态代码 '1'”。 有没有办法正确地做到这一点? (即从我的控制器方法转到另一条路线?)


您可以使用redirect() method.

return redirect()->route('route.name')->with(['email'=> '[email protected]']);

自从使用with() with redirect()会将“电子邮件”添加到会话中(不是请求)。然后使用以下命令检索电子邮件:

request()->session()->get('email')
//Or
session('email')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从控制器方法重定向到路由 的相关文章