gpt4 book ai didi

php - 如何从 Controller 方法重定向到路由

转载 作者:可可西里 更新时间:2023-11-01 00:08:12 25 4
gpt4 key购买 nike

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

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

    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’”。无论如何要正确地做到这一点? (即从我的 Controller 方法转到另一条路线?)

最佳答案

您可以使用redirect() 方法。

return redirect()->route('route.name')->with(['email'=> 'abc@xys.com']);

因为将 with()redirect() 一起使用,将向 session 添加“电子邮件”(不是请求)。然后检索电子邮件:

request()->session()->get('email')
//Or
session('email')

关于php - 如何从 Controller 方法重定向到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45440446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com