从子方法重定向


Laravel 5 Redirect from a sub-method

我正在使用Socialite从facebook获取用户信息。一切顺利,但我的重定向不起作用

Sub-routes

我读到这是不可能做一个重定向从子方法,或任何不在你的路由中的方法。

但是在我登录用户之后,我还能如何重定向用户呢?

facebook握手成功后,我的URL是这样的

http://tmdb.app/auth/login/facebook?code=AQBTKNZIxbfdBruAJBqZ8xx9Qnz...

class SocialController extends Controller {
    public function login(Authenticate $authenticate, Request $request)
    {
        return $authenticate->execute($request->has('code'), $this);
    }
    public function userHasLoggedIn($data)
    {
        $user = User::where('provider_id', $data->id)->first();
        if( !$user )
        {
            $user = User::create([
                'name' => $data->name,
                'email' => $data->email,
                'provider' => 'facebook',
                'provider_id' => $data->id
            ]);
        }
        // NOT WORKING!
        return redirect('test');
    }
}

您的登录函数应该处理重定向。

我猜如果用户成功登录,execute返回$data,否则返回false。

class SocialController extends Controller {
    public function login(Authenticate $authenticate, Request $request)
    {
        if($data = $authenticate->execute($request->has('code'), $this))
        {
            $user = User::where('provider_id', $data->id)->first();
            // maybe delegate the user creation to another class/service?
            if( !$user )
            {
                $user = User::create([
                    'name' => $data->name,
                    'email' => $data->email,
                    'provider' => 'facebook',
                    'provider_id' => $data->id
                ]);
            }
        return redirect('test');
        }
    return redirect('fail_view');
    }
}

可以使用Laravel子方法中的PHP header函数来实现。我试了一下,效果很好。希望它能帮到你。

<>之前 // You can using the following code $url= url("about-laravel"); header("Location:" . $url); exit; // Or using the following code to redirect and keep set flash message $result= $this->yourMethod(); // return redirect($this->route)->with('flash_message', 'I''m Flash Message'); for TRUE or NULL for false if( $result ){ return $result; }