使用Socialite通过Facebook进行身份验证时出现以下错误。使用Laravel 5.2,这是我第一次尝试实现Socialite。有什么想法吗?
FatalErrorException in AbstractProvider.php line 134:
Call to a member function set() on a non-object
路线: -
Route::get('/login', 'AuthController@login');
AuthController.php: -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
public function login()
{
return \Socialite::with('facebook')->redirect();
}
}
services.php设置如下.env文件中的详细信息: -
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT'),
],
此处报告的错误相同,但没有回复: - https://laracasts.com/discuss/channels/laravel/laravel-socialite-session-errors-in-52/replies/125233
答案 0 :(得分:8)
有同样的问题......
看起来您必须将路由放在MiddleWare中,因为它包含AbstractProvider.php中需要的会话创建134
$this->request->getSession()->set('state', $state = Str::random(40));
这就是我的代码routes.php现在的样子(并且有效):
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('admin', function () {
return view('admin_template');
});
Route::get('test', 'TestController@index');
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
Route::get('auth/google', 'Auth\AuthController@redirectToProvider');
Route::get('auth/google/callback', 'Auth\AuthController@handleProviderCallback');
});
答案 1 :(得分:0)
您收到此错误的原因是因为没有会话已启动。您必须将$middleware
中间件分配给您的呼叫路由。
在早期版本的Laravel中,这已在app/Http/Kernel.php
属性中分配,可在文件(ListIterator<E> it = listIterator(); it.hasNext(); )
中找到。
答案 2 :(得分:0)
在你的route.php中尝试这段代码
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
Route::get('/redirect', 'SocialAuthController@redirect');
Route::get('/callback', 'SocialAuthController@callback');
});