Laravel 5.3でのMulti-Authを行おうとしています。
http://qiita.com/zaburo/items/cd3f2ccfb5c2d0fba340
や
http://qiita.com/kimama1997/items/bbe4d17af21860de1930#_reference-1e73c2842c6379c2af3f
などを参考にして、
config/auth.php
1<?php 2 3return [ 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Authentication Defaults 8 |-------------------------------------------------------------------------- 9 | 10 | This option controls the default authentication "guard" and password 11 | reset options for your application. You may change these defaults 12 | as required, but they're a perfect start for most applications. 13 | 14 */ 15 16 'defaults' => [ 17 'guard' => 'web', 18 'passwords' => 'users', 19 ], 20 21 /* 22 |-------------------------------------------------------------------------- 23 | Authentication Guards 24 |-------------------------------------------------------------------------- 25 | 26 | Next, you may define every authentication guard for your application. 27 | Of course, a great default configuration has been defined for you 28 | here which uses session storage and the Eloquent user provider. 29 | 30 | All authentication drivers have a user provider. This defines how the 31 | users are actually retrieved out of your database or other storage 32 | mechanisms used by this application to persist your user's data. 33 | 34 | Supported: "session", "token" 35 | 36 */ 37 38 'guards' => [ 39 'web' => [ 40 'driver' => 'session', 41 'provider' => 'users', 42 ], 43 44 'api' => [ 45 'driver' => 'token', 46 'provider' => 'users', 47 ], 48 //追加 49 'user' => [ 50 'driver' => 'session', 51 'provider' => 'users', 52 ], 53 //追加 for admin 54 'admin' => [ 55 'driver' => 'session', 56 'provider' => 'admins', 57 ], 58 ], 59 60 /* 61 |-------------------------------------------------------------------------- 62 | User Providers 63 |-------------------------------------------------------------------------- 64 | 65 | All authentication drivers have a user provider. This defines how the 66 | users are actually retrieved out of your database or other storage 67 | mechanisms used by this application to persist your user's data. 68 | 69 | If you have multiple user tables or models you may configure multiple 70 | sources which represent each model / table. These sources may then 71 | be assigned to any extra authentication guards you have defined. 72 | 73 | Supported: "database", "eloquent" 74 | 75 */ 76 77 'providers' => [ 78 'users' => [ 79 'driver' => 'eloquent', 80 'model' => App\User::class, 81 ], 82 83 //追加 for admin 84 'admins' => [ 85 'driver' => 'eloquent', 86 'model' => App\Admin::class, 87 ], 88 89 // 'users' => [ 90 // 'driver' => 'database', 91 // 'table' => 'users', 92 // ], 93 ], 94 95 /* 96 |-------------------------------------------------------------------------- 97 | Resetting Passwords 98 |-------------------------------------------------------------------------- 99 | 100 | You may specify multiple password reset configurations if you have more 101 | than one user table or model in the application and you want to have 102 | separate password reset settings based on the specific user types. 103 | 104 | The expire time is the number of minutes that the reset token should be 105 | considered valid. This security feature keeps tokens short-lived so 106 | they have less time to be guessed. You may change this as needed. 107 | 108 */ 109 110 'passwords' => [ 111 'users' => [ 112 'provider' => 'users', 113 'table' => 'password_resets', 114 'expire' => 60, 115 ], 116 117 //追加 for admin 118 'admin' => [ 119 'provider' => 'admins', 120 'email' => 'auth.emails.password', 121 'table' => 'password_resets', 122 'expire' => 60, 123 ], 124 ], 125 126];
を作成し、
routes/web.php
1<?php 2 3/* 4|-------------------------------------------------------------------------- 5| Web Routes 6|-------------------------------------------------------------------------- 7| 8| This file is where you may define all of the routes that are handled 9| by your application. Just tell Laravel the URIs it should respond 10| to using a Closure or controller method. Build something great! 11| 12*/ 13 14Route::get('/', function () { 15 return view('welcome'); 16}); 17 18 19//Auth::routes(); 20Route::group(['middleware' => ['web']], function() { 21 22// Login Routes... 23 Route::get('login', ['as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm']); 24 Route::post('login', ['as' => 'login.post', 'uses' => 'Auth\LoginController@login']); 25 Route::post('logout', ['as' => 'logout', 'uses' => 'Auth\LoginController@logout']); 26 27// Registration Routes... 28 Route::get('register', ['as' => 'register', 'uses' => 'Auth\RegisterController@showRegistrationForm']); 29 Route::post('register', ['as' => 'register.post', 'uses' => 'Auth\RegisterController@register']); 30 31// Password Reset Routes... 32 Route::get('password/reset', ['as' => 'password.reset', 'uses' => 'Auth\ForgotPasswordController@showLinkRequestForm']); 33 Route::post('password/email', ['as' => 'password.email', 'uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail']); 34 Route::get('password/reset/{token}', ['as' => 'password.reset.token', 'uses' => 'Auth\ResetPasswordController@showResetForm']); 35 Route::post('password/reset', ['as' => 'password.reset.post', 'uses' => 'Auth\ResetPasswordController@reset']); 36 37 38}); 39 40Route::group(['middleware' => ['auth:admin']], function() { 41 42// Login Routes... 43 Route::get('admin/login', ['as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm']); 44 Route::post('admin/login', ['as' => 'login.post', 'uses' => 'Auth\LoginController@login']); 45 Route::post('admin/logout', ['as' => 'logout', 'uses' => 'Auth\LoginController@logout']); 46 47// Registration Routes... 48 Route::get('admin/register', ['as' => 'register', 'uses' => 'Auth\RegisterController@showRegistrationForm']); 49 Route::post('admin/register', ['as' => 'register.post', 'uses' => 'Auth\RegisterController@register']); 50 51// Password Reset Routes... 52 Route::get('admin/password/reset', ['as' => 'password.reset', 'uses' => 'Auth\ForgotPasswordController@showLinkRequestForm']); 53 Route::post('admin/password/email', ['as' => 'password.email', 'uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail']); 54 Route::get('admin/password/reset/{token}', ['as' => 'password.reset.token', 'uses' => 'Auth\ResetPasswordController@showResetForm']); 55 Route::post('admin\admin/password/reset', ['as' => 'password.reset.post', 'uses' => 'Auth\ResetPasswordController@reset']); 56 57 58}); 59 60 61 62 63 64Route::get('/home', 'HomeController@index'); 65Route::get('/admin/home', 'Admin\AdminHomeController@index');
のように作成しました。
デフォルト(users DB使用)でログインする処理は、「php artisan make:auth」で作成し、guardsがadminの処理は、デフォルトの処理のプログラムをコピーして使用しています。
HomeController.php を Admin/AdminHomeController.php などにしています。
http://hoge.com/login
でのログインは、うまくいくのですが、
http://hoge.com/admin/login でのログインは、http://hoge.com/login
へリダイレクトをされてしまったりし、うまくいきません。
恐らく、木本的なことがわかっていないのだと思います。
なにか、良い参考になるサイトなどはありませんでしょうか。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/17 13:05
2016/11/17 13:24