微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

    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()方法.

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

因为使用with()和redirect()会将“email”添加到会话中(而不是请求).然后检索电子邮件

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐