Laravel5.4框架使用socialite实现github登录的方法
本文实例讲述了Laravel5.4框架使用socialite实现github登录的方法。分享给大家供大家参考,具体如下:
1.安装laravel5.4
composer create-project laravel/laravel zcms 5.4
2.安装Socialite
composer require laravel/socialite
3.配置
编辑config/app.php
'providers' => [ // 其它服务提供者... 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.申请github oauth apps
①.登录github->settings->OAuth Apps
②.填写Homepage URL(网站域名http://www.zcms.site),Authorization callback URL(回调路径http://www.zcms.site/github/login)
③.复制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'); //这里为刚才的回调路径
②.创建Controller
App\Http\Controllers创建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.见证奇迹吧
访问www.zcms.site/login。竟然跳转到了github,确认之后返回www.zcms.site/github/login?code=乱七八糟
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
相关文章
Laravel 5 框架入门(二)构建 Pages 的管理功能
这篇文章主要介绍了Laravel 5 框架入门的第二篇文章,给大家讲解的是构建 Pages 的管理功能,十分的详细,有需要的小伙伴可以参考下。2015-04-04
PHP date()函数警告: It is not safe to rely on the system解决方法
这篇文章主要介绍了PHP date()函数警告: It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了两种方法来解决这个问题,需要的朋友可以参考下2014-08-08
Laravel 5框架学习之日期,Mutator 和 Scope
这篇文章主要介绍了Laravel 5框架学习之日期,Mutator 和 Scope的相关资料,需要的朋友可以参考下2015-04-04
PHP 中 var_export、print_r、var_dump 调试中的区别
这篇文章主要介绍了PHP 中 var_export、print_r、var_dump 调试中的区别,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2018-06-06


最新评论