This is my first time implementing OAuth to my projects. I found a walkthrough on github for laravel-5 oriceon/oauth-5-laravel. I followed all the steps correctly. However when I get to the controller function I get an error saying:
Call to undefined method Illuminate\Support\Facades\Request::get()
Here is my controller function:
public function loginWithFacebook(Request $request)
{
// get data from request
$code = $request->get('code');
// get fb service
$fb = \OAuth::consumer('Facebook');
// check if code is valid
// if code is provided get user data and sign in
if ( ! is_null($code))
{
// This was a callback request from facebook, get the token
$token = $fb->requestAccessToken($code);
// Send a request with it
$result = json_decode($fb->request('/me'), true);
$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
echo $message. "<br/>";
//Var_dump
//display whole array.
dd($result);
}
// if not ask for permission first
else
{
// get fb authorization
$url = $fb->getAuthorizationUri();
// return to facebook login url
return redirect((string)$url);
}
}
In the app you can see that i did add the correct provider and alias:
'OAuth' => Artdarek\OAuth\Facade\OAuth::class,
Artdarek\OAuth\OAuthServiceProvider::class,
In my view I call the route that leads to the correct controller function and I keep arriving to this error. What could it be that does this? Should the function be calling to the provider or something? Thanks for looking at this Stack!
First up, I hope your view isn't calling a route- that's backwards. Routes are used immediately to determine the controller, which is then used to determine and respond with the proper view.
... That aside, Request
is the name of a facade in Laravel. That's why the error message says it's looking for the get() method on the Illuminate\Support\Facades\Request class. You'll want to namespace the Request class you're using so that it's able to use the correct get() method. Depending on your version, I do this with use Illuminate\Http\Request;
at the top of my controller file (immediately after the namespace declaration for the controller).