首页 文章

Facebook社交名媛登录Laravel 5.1

提问于
浏览
6

我已按照http://laravel.com/docs/5.1/authentication中的所有步骤在Laravel 5.1上使用facebook进行社交登录 .

这是我遵循的步骤:

1 - 使用Laravel根项目中的命令:

composer require laravel/socialite

2 - 在config / app.php配置文件中注册Laravel \ Socialite \ SocialiteServiceProvider:

'providers' => [
    // Other service providers...

    Laravel\Socialite\SocialiteServiceProvider::class,
],

3 - 将Socialite外观添加到应用程序配置文件中的别名数组:

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

4 - 在config / services.php中添加facebook服务:

'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => 'http://homestead.app/auth/facebook/callback',
    ],

5 - 向我的AuthController.php添加方法redirectToProvider()和handleProviderCallback():

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Redirect path after successful login
     *
     * @var string
     */
    protected $redirectPath = '/greeting';

    /**
     * Redirect path after failed login
     *
     * @var string
     */
    protected $loginPath = '/auth/login';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     * Redirect the user to the Facebook authentication page.
     *
     * @return Response
     */


    public function redirectToProvider()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Obtain the user information from Facebook.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('facebook')->user();

        dd($user);
    }
}

我想强调一下,我在app / Http / Controllers / Auth /文件夹中创建了AuthController.php,因为它要求创建路由:'Auth \ AuthController @ redirectToProvider''Auth \ AuthController @ handleProviderCallback'

6 - 我添加了路线:

Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
Route::get('auth/facebook/callback', 'Auth\AuthController@handleProviderCallback');

现在,当我在登录页面中单击"Login with facebook"时,它会转到http://homestead.app/auth/facebook并收到以下错误:

AuthController.php第90行中的FatalErrorException:未找到类“社交名称”

我试图遵循codeanchor guide但它是laravel 5.0(我需要在laravel 5.1上使用它)

1 回答

  • 2

    最后,我已经解决了删除所有内容并从一开始就重新安装Homestead的问题

相关问题