hoanghiep/auth-google

使用 Google 账户登录

dev-master 2016-07-15 09:44 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:36:52 UTC


README

1. 在 routes.php 文件中添加新的路由

  • 初始化提供者文件
  • 在未包含内容的包中初始化 routes.php 文件
  • 初始化用于注册包的提供者类文件

通过 boot 方法将 routes.php 添加到 Laravel 中

if (! $this->app->routesAreCached()) { require DIR.'/routes.php'; }

2. 创建配置文件,包含需要添加到配置中的数组示例

[ 'driver' => 'local', 'root' => app_path('/'), 'visibility' => 'public', ] ]; # 3. 在 register 方法中添加配置到 Laravel 的配置文件,使用文件 config 的 key : public function register() { $this->mergeConfigFrom( __DIR__.'/config/filesystems.php', 'filesystems.disks' ); } # 4. 通过在包的 route 中编写代码,将新的路由添加到 app/Http/routes.php 文件中 : Route::get('hoanghiep/add-routes',function(){ if(Storage::disk('h3')->exists("Http/routes.php")){ Storage::disk("h3")->append('Http/routes.php', 'Route::get(\'auth/google\',["middleware"=>"web", "uses"=> \'Auth\GoogleController@redirectToProvider\']); '); Storage::disk("h3")->append('Http/routes.php', 'Route::get(\'auth/google/callback\', ["middleware"=>"web","uses"=> \'Auth\GoogleController@handleProviderCallback\']); '); } }); // 在执行 hoanghiep/add-routes 后,这段代码将检查 app/Http/routes.php 中的 routes.php 文件是否存在,如果存在,则添加新的路由 'auth/google' 和 'auth/google/callback' 到该文件。 # 5. 创建处理这两个文件的控制器文件。在提供者类中的 boot() 方法中添加以下代码 : $this->publishes([ __DIR__.("/Auth/GoogleController.php")=> app_path("/Http/Controllers/Auth/GoogleController.php"), ]) 运行命令以发布控制器文件 : php artisan vendor:publish 因此,运行命令后,控制器将被创建。添加 Google 配置文件,通过在包中创建一个名为 service.php 的文件来实现,其中包含 : [ 'client_id' => '15561597118-vv58u5dtkm10uni08kv471ltjb1od40m.apps.googleusercontent.com', 'client_secret' => '_1Ojx13UdmPMkTadUrClA9Cw', 'redirect' => 'https:///laravel/auth/google/callback', ] ]; 然后将此代码添加到提供者中 : $this->mergeConfigFrom( __DIR__.'/config/services.php', 'services' ); 这样,文件配置已成功合并到 config 文件中。通过访问方法 boot 或 route,运行 dd(config('services')); 或 dd(config('services.google')); 来检查结果。 + 若要自定义 Google 服务信息,请访问 package/config/services.php 文件中的 'services' 文件。+ 若要测试功能,请运行两个 URL auth/google 和 auth/google/callback。如果 URL 不存在,请运行以创建该 URL 的命令:hoanghiep/add-routes