Php Laravel-将用户重定向到配置文件页面

Php Laravel-将用户重定向到配置文件页面,php,laravel,authentication,laravel-5,Php,Laravel,Authentication,Laravel 5,我试图重定向用户,如果他第一次登录到step2页面,我尝试了以下方法 public function postLogin(Request $request){ $credentials = $request->only('login_email', 'login_password'); $credential = ['email'=> $credentials['login_email'], 'password' => $credentials['lo

我试图重定向用户,如果他第一次登录到step2页面,我尝试了以下方法

    public function postLogin(Request $request){

    $credentials = $request->only('login_email', 'login_password');
    $credential = ['email'=> $credentials['login_email'], 'password' => $credentials['login_password']];
    if (Auth::attempt($credential)) {
        // if profile not set, redirect to step 2 page
        if(Auth::user()->first_login) {

          return  $this->getStep2(Auth::user()->id);


        }
}
但它告诉我

{"login":true}
我的
getStep2()
如下

    public function getStep2($id){
    $genres = Track::orderBy('genre', 'asc')->groupBy('genre')->get();
    $countries = Country::all();
    $categories = Category::where('parent_id',  '')->get();
    $user_id = $id;
    return view('users.step2', compact('genres', 'countries', 'categories', 'user_id'));
}

如果你想重定向,你应该

return redirect('users/step2');
然后,在你的
routes.php
中有这个路由

Route::get('users/step2', 'UserController@getStep2');
注意,实际上不需要将用户id作为参数传递,因为可以使用
Auth
facade访问它



如果您试图做的实际上是加载一个视图,那么您的方法应该做到这一点。我猜您的一个方法在到达
return
语句之前就结束了。

您也可以使用
redirect()->action(…)
方法

public function postLogin(Request $request) {

    $credentials = $request->only('login_email', 'login_password');
    $credential = ['email' => $credentials['login_email'], 'password' => $credentials['login_password']];

    if (Auth::attempt($credential)) {
        // if profile not set, redirect to step 2 page
        if (Auth::user()->first_login) {
            return redirect()->action('Auth\AuthController@getStep2');

        }
    }

    return redirect('/');
}
请注意,您仍然必须为此页面创建路由

Route::get('step-two', ['uses' => 'Auth\AuthController@getStep2']);

要访问当前用户,可以使用
Auth::user()
方法。

必须返回此函数调用的返回值
$this->getStep2(Auth::user()->id)
在您的
postLogin
方法中仍然一样,我添加了return,您真的在函数调用中输入了if块吗?即使您的条件不匹配,您也应该返回(默认)值/操作/视图。是的,我做了一些调试,它确实进入了if块
public function postLogin(Request $request) {

    $credentials = $request->only('login_email', 'login_password');
    $credential = ['email' => $credentials['login_email'], 'password' => $credentials['login_password']];

    if (Auth::attempt($credential)) {
        // if profile not set, redirect to step 2 page
        if (Auth::user()->first_login) {
            return redirect()->action('Auth\AuthController@getStep2');

        }
    }

    return redirect('/');
}