?? ? ?? ??? ??? ????? ???? ?? ???? ????? ??????? ?? ? ??? ??? ?? ?? ??? ?? ? ???? ????. ??? ?? ? ???? ???? ?? ?? ?3? ?? ???? ???? ? ???????? ????? ???? ?? ? ??? ?????. ?? ???? PHP ?????? Laravel? ??? ?? ??? ?? ??? ????? ?? ??? ??? ???? ?? ?? ?????. ? ????? Laravel? ???? ?? ??? ??? ???? ??? ???? ??? ??? ?? ?? ?? ??? ?????.
1. ??? ??? ?? ??
Laravel??? ?? ??? ?? ??? socialite
? ???? ?? ??? ??? ??? ? ????. ?????
? Facebook, Google, Twitter, WeChat ?? ??? ??? ?? ??? ????? ?????. ??? socialite
? ???? ?? ??? ??? ???? ?? ??? ??? ??? ???? ???. socialite
來實現(xiàn)第三方登錄功能。而socialite
支持的授權(quán)服務提供商多種多樣,包括Facebook、Google、Twitter、WeChat等。因此,在使用socialite
實現(xiàn)第三方登錄功能之前,我們需要配置服務商信息。
以GitHub為例,通過創(chuàng)建并授權(quán)一個OAuth應用程序,我們可以獲取到客戶端ID和客戶端密鑰兩個信息。具體的獲取方式如下:
- 訪問GitHub的網(wǎng)站,并登錄賬戶。
- 進入GitHub的設置頁面,選擇"Developer settings"選項卡,點擊"OAuth Apps",然后選擇"New OAuth App"。
- 填寫應用程序的詳細信息,包括應用名稱、應用主頁URL、授權(quán)回調(diào)URL等。
- 提交并創(chuàng)建應用程序。創(chuàng)建成功后,可以在GitHub頁面中找到該應用程序的客戶端ID和客戶端密鑰等信息。
有了這些信息之后,我們就可以創(chuàng)建一個名為/config/services.php
的Laravel配置文件,并在其中添加以下內(nèi)容:
'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => 'http://localhost:8000/login/github/callback', ],
其中,github
是我們要使用的服務提供商的名稱,client_id
和client_secret
是我們在申請OAuth應用程序時獲取到的客戶端ID和客戶端密鑰,redirect
是OAuth回調(diào)URL,它是我們在創(chuàng)建應用程序時填寫的授權(quán)回調(diào)URL。這里我們設置為localhost:8000/login/github/callback,后面我們將會在路由文件中創(chuàng)建這個路由。
二、創(chuàng)建路由
接下來,我們需要在Laravel應用程序的路由文件中創(chuàng)建路由,實現(xiàn)通過GitHub登錄的功能。在這里,我們可以使用Laravel的Route
類來創(chuàng)建路由。
第一步,我們需要創(chuàng)建一個授權(quán)跳轉(zhuǎn)路由,由它負責將用戶跳轉(zhuǎn)至GitHub登錄頁面。在路由文件中添加以下代碼:
Route::get('login/github', function () { return Socialite::driver('github')->redirect(); });
其中,Socialite::driver('github')
調(diào)用socialite
授權(quán)包的driver
方法,獲取到GitHub服務提供商的實例。而redirect
方法則實現(xiàn)了OAuth授權(quán)跳轉(zhuǎn),將用戶重定向到GitHub的認證授權(quán)頁面上。
第二步,我們創(chuàng)建一個授權(quán)回調(diào)路由,由它負責獲取GitHub服務提供商返回的用戶信息,并進行處理。在路由文件中添加以下代碼:
Route::get('login/github/callback', function () { $user = Socialite::driver('github')->user(); // 處理用戶信息 });
其中,Socialite::driver('github')
調(diào)用socialite
授權(quán)包的driver
方法,獲取到GitHub服務提供商的實例。而user
方法則實現(xiàn)了OAuth授權(quán)獲取用戶信息的過程,它會從GitHub服務提供商獲取到用戶的信息,該信息將以$user
變量形式被返回。
在獲取到$user
變量之后,我們便可以對用戶信息進行處理了。由于每個第三方服務提供商返回的用戶信息格式略有不同,因此這里就不做詳細展開了。
三、創(chuàng)建公用方法
為了方便管理和使用,我們可以在app/Http/Controllers/Auth/LoginController.php
控制器中創(chuàng)建GitHub
方法,供其他方法調(diào)用。
在該方法中,我們首先需要使用Socialite::driver('github')->redirect()
方法將用戶重定向到GitHub登錄頁面上。而在獲取用戶信息的回調(diào)過程中,我們可以在GitHubCallback
方法中對用戶信息進行處理。
下面是一個基本的示例代碼:
<?php namespace AppHttpControllersAuth; use AppHttpControllersController; use Socialite; class LoginController extends Controller { /** * 重定向到GitHub登錄頁面 * * @return Response */ public function GitHub() { return Socialite::driver('github')->redirect(); } /** * 從GitHub獲取用戶信息 * * @return Response */ public function GitHubCallback() { $user = Socialite::driver('github')->user(); dd($user); } }
四、使用擴展包
如果你覺得上述方法過程繁瑣,可以使用社交化登錄授權(quán)擴展包laravel/socialite
,這個擴展包已經(jīng)對各大服務提供商進行了封裝,并提供了相對應的命令行工具來構(gòu)造授權(quán)服務提供商。
- 安裝
laravel/socialite
在終端中使用composer安裝laravel/socialite
擴展包:
composer require laravel/socialite
- 配置服務商信息
在Laravel的配置文件config/services.php
中,添加需要使用的服務商信息,下面以GitHub為例:
'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => '/auth/github/callback', ],
- 創(chuàng)建授權(quán)路由
在routes/web.php
- GitHub ????? ???? ??? ??????.
- GitHub ?? ???? ???? "??? ??" ?? ???? "OAuth ?"? ??? ?? "? OAuth ?"? ?????.
- ?????? ??, ?????? ???? URL, ?? ?? URL ?? ???? ??????? ?? ??? ??? ?????.
- ???? ???? ?????. ????? ???? ??????? ????? ID, ????? ? ?? ??? GitHub ????? ??? ? ????.
/config/services.php
?? Laravel ?? ??? ??? ??? ?? ???? ??? ? ????. ??Route::get('/auth/github', 'AuthLoginController@GitHub');?? ??
github
? ??? ????? ??? ???? ????, client_id
? client_secret
? OAuth ??????? ??? ? ?? ?????. ????? ID? ????? ????, redirect
? OAuth ?? URL?, ??????? ??? ? ??? ?? ?? URL???. ???? ?? localhost:8000/login/github/callback?? ?????. ??? ??? ??? ? ??? ??? ????. ????2. ?? ?????????? GitHub? ?? ??? ??? ???? ?? Laravel ??????? ??? ??? ??? ???? ???. ???? Laravel? Route
???? ???? ??? ??? ? ????. ????? ?? ????? ???? GitHub ??? ???? ?????? ?? ???? ??? ???? ???. ??? ??? ?? ??? ?????: ??Route::get('/auth/github/callback', 'AuthLoginController@GitHubCallback');?? ? ?
Socialite::driver('github')
? socialite
? driver
???? ?????. code> ?? ???? ?? GitHub ??? ???? ????? ?????. redirect
???? OAuth ?? ??? ???? ???? GitHub? ?? ? ?? ???? ???????. ????? ?? ????? GitHub ??? ???? ??? ??? ??? ??? ???? ?? ?? ?? ??? ????. ??? ??? ?? ??? ?????: ??use Socialite; class LoginController extends Controller { public function GitHub() { return Socialite::driver('github')->redirect(); } public function GitHubCallback() { $user = Socialite::driver('github')->user(); dd($user); } }?? ? ?
Socialite::driver('github')
? socialite
? driver
???? ?????. code> ?? ???? ?? GitHub ??? ???? ????? ?????. user
???? OAuth ??? ?? ??? ??? ?? ????? ????, ?? $user</code ???? ???? GitHub ??? ?????? ??? ??? ????. > ?? ??. ????<code>$user
??? ?? ? ??? ??? ??? ? ????. ?3? ??? ?????? ???? ??? ??? ??? ??? ??? ??? ???? ??? ??? ?????. ????3. ?? ??? ???????? ? ??? ???? ?? ?? app/Http/Controllers/Auth/LoginController.php
?? GitHub
???? ??? ? ????. ????? ???? ?? ???? ?????. ????? ????? ?? Socialite::driver('github')->redirect()
???? ???? ???? GitHub ??? ???? ?????? ???. ??? ??? ?? ?? ???? GitHubCallback
????? ??? ??? ??? ? ????. ??????? ?? ?? ?????. ??rrreee?? 4. ?? ??? ???? ????? ??? ????? ????? ?? ??? ?? ?? ??? laravel/socialite
? ????? ???, ? ?? ????? ?? ??? ???? ??????? ?? ??? ??? ???? ??? ??? ???? ??? ? ????. ??laravel/socialite
??
laravel/socialite
?? ???? ?????: ??rrreee - ??? ??? ?? ??
config/services.php
?? ???? ?? ??? ??? ??? ?????. , ??? ?? GitHub? ?? ?? ?????. ??rrreee- ?? ?? ???
routes/web.php
??? ???? , ??? ?? ??? ?????: ??Route::get('/auth/github', 'AuthLoginController@GitHub');
- 創(chuàng)建回調(diào)路由
在routes/web.php
路由文件中,創(chuàng)建一個授權(quán)回調(diào)路由:
Route::get('/auth/github/callback', 'AuthLoginController@GitHubCallback');
- 創(chuàng)建控制器
在app/Http/Controllers/Auth
目錄下,創(chuàng)建一個名為LoginController.php
的控制器??刂破髦刑砑臃椒?code>GitHub和GitHubCallback
實現(xiàn)授權(quán)和回調(diào)過程,具體代碼如下:
use Socialite; class LoginController extends Controller { public function GitHub() { return Socialite::driver('github')->redirect(); } public function GitHubCallback() { $user = Socialite::driver('github')->user(); dd($user); } }
至此,我們已經(jīng)成功使用laravel/socialite
擴展包實現(xiàn)了第三方登錄的功能。
總結(jié):
本文介紹了如何使用Laravel框架的社交化登錄授權(quán)包socialite
,在實現(xiàn)第三方登錄功能方面變得尤為便利。通過配置服務商信息、創(chuàng)建路由以及使用擴展包等方法,我們可以輕松地實現(xiàn)通過GitHub登錄的功能。希望這篇文章能夠?qū)Ω魑婚_發(fā)人員有所幫助。
? ??? Laravel? ???? ?? ??? ??? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

Laravel? eloquentscopes? ?? ??? ??? ??? ?????? ?? ?? ??? ????? ?????. 1. ?? ??? ???? ???? ???? ???? Post :: published (); 2. ??? ??? ?? ??? ???? ???? ?? ??? ?? ?? ?? ??? ???? ???? ??? ?????? ??? ???? ???????. 3. ????? ?? ?? ?? ??? ??? ?? ?? ??? ?? ? ? ??? ?? ? ? ?? ?? ??? ?????. 4. ?? ??? ? ??? ?? ???? ? ??? ? ?? ??, ?? ??, ?? ???? ? ?? ?????????.

CreateAhelpers.phpfileInapp/helperswithCustOmFunctionsikeFormatPrice, isactiveroute, andisAdmin.2.addTheFileTothe "??"sectionOfcomposer.jsonUnderAutoLoad.3.runcomposerDump-AUTOLOADTOMAKETHINGTICTIONSGLOBELYAVAILABLE.4.USETHEHELPERFUNCUNTION

?? ?? ?? : ?? ????? PHP? ?? Error_Log ()? ??? ? ????. ????? ???? ??? ?? ??? ?????? ???? ?? ??? ? ?? ??? ???? ??? ?? ???, ??, ?? ? ?? ? ?? ?? ??? ???? ??? ??????. 2. ??? ?? ?? : ??? ??? ??? ??? ? ??? ?? ??? ??? ?? ??? ??? ??????? ??????. MySQL/PostgreSQL? ???? ??? ? ???? ??????. Elasticsearch Kibana? ? ???/? ???? ?????. ???, ??? ?? ? ??? ? ?? ??? ?? ??????. 3. ?? ? ?? ????? : ??, ???, ?? ? ??? ??? ??????. Kibana? ?? ????? PHP ??? ?? ?? ?????? ???? ???? ?????? ???? ??? ? ?? ??? ??? ? ????.

USEMOCKERYFORCUSTOMDENCENTICESBYSETEXPINTIONSWITHSHOULDRECEIVE ()

??, ??, ?? ?? ? ?? ??? ???? ?? ??? ?? ? ?? ???? ?????. 2. ?? ???? ???? ?? ??? ??? SONGSTOMONY ? HASMANY ?? ??; 3. ?? ? ? ?? ? ?? ??? ????? (?? ???? ?? ??? ? ??). 4. ?? ? ?? ??? ???? ?? ??? ???? ?? ? ?? ??? ???? ?? ??? ?????. 5. ?? ???? ??? ?? (?? ??)? ???? ?? ????? ??????. 6. ?? ??? ?? ??? ???? Laravel Signature URL? ???? ??? ??????. 7. ? ?? ?? ? ? ?? ??? ?? ?? ??? ?? ??? ?????. ?????? ??, ?? ?? ??? ??????????.
