質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

1回答

3896閲覧

Laravel 5.3でのMulti-Authのやり方について

gomengo

総合スコア51

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

1クリップ

投稿2016/11/15 13:16

編集2016/11/15 13:34

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
へリダイレクトをされてしまったりし、うまくいきません。
恐らく、木本的なことがわかっていないのだと思います。

なにか、良い参考になるサイトなどはありませんでしょうか。

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

大きな理由としては
Route::group(['middleware' => ['auth:admin']], function() {
の中に
Route::get('admin/login', ['as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm']);
が含まれているからです。
middlewareが動かなくて良いログインページは外に出しておく必要があります。

また、対象のadminページにアクセスしているのに、/loginに遷移してしまう件を解決するには、Exceptions/Handler.phpのunauthenticatedメソッドの内容を修正する必要があります。

おそらく、

protected function unauthenticated($request, AuthenticationException $exception) { if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); } // $exception->guardsには通ってきたguardが配列で入っているので、adminが入っていればadmin用のログインページへ if (in_array('admin', $exception->guards())) { return redirect()->guest('admin/login'); } return redirect()->guest('login'); }

このように変更することで、対応することができます。
このあたりの処理は、Laravelのバージョンによって度々変更が加えられているので追うのが大変ですね。

また関係ありませんが、Route::group(['middleware' => ['web']], function() {は必要ありません。
なぜならwebルーティングが適用されているファイルだからです。
なぜ不要なのかは、Providers\RouteServiceProvider.phpのmapWebRoutesメソッド見ていただければ分かるかと思います。定義されてからrequireされます。こちらもバージョン5.3からこのような仕様になっており、それ以下のバージョンでは定義する必要があります。

投稿2016/11/16 12:58

fagai

総合スコア2158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gomengo

2016/11/17 13:05

ありがとうございます。色々試してみます。 まだまだ、理解できていない部分があるので、理解できるように頑張ります。
fagai

2016/11/17 13:24

頑張ってくださいねー。 簡単には、Route::groupで定義したmiddlewareはリクエストの最初に呼ばれます。authミドルウェアを指定している空間に書かれたルーティングは、認証されていないとアクセスすることができません。 なので、/admin/loginページにアクセスしようとしてもアクセスができない状態です。そのため、ログインページはauthミドルウェアが通らない箇所に書く必要があるわけです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問