本文實(shí)例講述了Laravel5.4框架使用socialite實(shí)現(xiàn)github登錄的方法。分享給大家供大家參考,具體如下:
1.安裝laravel5.4
composer create-project laravel/laravel zcms 5.4
2.安裝Socialite
composer require laravel/socialite
3.配置
編輯config/app.php
'providers' => [
// 其它服務(wù)提供者...
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]
編輯config/service.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT'),
],
4.申請(qǐng)github oauth apps
①.登錄github->settings->OAuth Apps
②.填寫Homepage URL(網(wǎng)站域名http://www.zcms.site),Authorization callback URL(回調(diào)路徑http://www.zcms.site/github/login)
③.復(fù)制client_id,client_secret到.env文件
GITHUB_CLIENT_ID=211a7aa4b9c5a3a4c10c
GITHUB_CLIENT_SECRET=2d3174561e440ed887a604f571aff9fa5bd84e44
GITHUB_REDIRECT=http://www.zcms.site/github/login
5.使用
①.添加路由
Route::get('/login', 'LoginController@github');
Route::get('/github.login', 'LoginController@githubLogin'); //這里為剛才的回調(diào)路徑
②.創(chuàng)建Controller
App\Http\Controllers創(chuàng)建LoginController.php
?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Socialite;
class LoginController extends Controller
{
public function github()
{
return Socialite::driver('github')->redirect();
}
public function githubLogin()
{
$user = Socialite::driver('github')->user();
dd($user);
}
}
6.見證奇跡吧
訪問(wèn)www.zcms.site/login。竟然跳轉(zhuǎn)到了github,確認(rèn)之后返回www.zcms.site/github/login?code=亂七八糟
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- Laravel 5.4重新登錄實(shí)現(xiàn)跳轉(zhuǎn)到登錄前頁(yè)面的原理和方法
- Laravel5.2使用Captcha生成驗(yàn)證碼實(shí)現(xiàn)登錄(session巨坑)
- laravel5.2實(shí)現(xiàn)區(qū)分前后臺(tái)用戶登錄的方法
- laravel5實(shí)現(xiàn)微信第三方登錄功能
- 基于Laravel5.4實(shí)現(xiàn)多字段登錄功能方法示例
- Laravel5.5 實(shí)現(xiàn)后臺(tái)管理登錄的方法(自定義用戶表登錄)
- 解決laravel5中auth用戶登錄其他頁(yè)面獲取不到登錄信息的問(wèn)題
- laravel 5.3 單用戶登錄簡(jiǎn)單實(shí)現(xiàn)方法
- Laravel 5.5 實(shí)現(xiàn)禁用用戶注冊(cè)示例
- Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法詳解