Laravel 5.5
開発環境cloud9です。
こちら、【Laravel】マルチログイン(ユーザーと管理者など)機能を設定してみたマルチ認証を実装しました。
上記ページのように、user と admin の認証を実装しています。
admin用の管理画面にて、ログインしたadmin情報(id)を取得したいのですが、エラーが出てしまいます。
以下、現在の状態です。
■routes/web.php
<?php /* |-------------------------------------------------------------------------- | アドミン側 認証不要 |-------------------------------------------------------------------------- */ Route::group(['prefix' => 'admin'], function() { Route::get('login', 'Admin\LoginController@showLoginForm')->name('admin.login'); Route::post('login', 'Admin\LoginController@login'); }); /* |-------------------------------------------------------------------------- | アドミン側 認証必要! |-------------------------------------------------------------------------- */ Route::group(['prefix' => 'admin', 'middleware' => 'auth:admin'], function() { Route::get('/', 'Admin\HomeController@index')->name('admin.home'); Route::get('home', 'Admin\HomeController@index')->name('admin.home'); Route::post('logout', 'Admin\LoginController@logout')->name('admin.logout'); //cats登録関連 基本形 Route::resource('cats', 'CatsController'); });
■Admin\HomeController
<?php namespace App\Http\Controllers\Admin; // Adminを追加 use Illuminate\Http\Request; use App\Http\Controllers\Controller; // これを追加 class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth:admin'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { $admin_id = Auth::id(); return view('admin.home', ['admin_id' => $admin_id]); } }
↑ function indexにて、admin_idを取得しようと、$admin_id = Auth::id(); return view('admin.home', ['admin_id' => $admin_id]);
としました。
しかし、damin/home 画面では以下のようにAuth Classがないとエラーが出てしまいます。
Class 'App\Http\Controllers\Admin\Auth' not found
ちなみに、以下、マルチ認証を実装するときに変更したところです。
■app/Exceptions/Handler.php
<?php namespace App\Exceptions; use Exception; use Illuminate\Auth\AuthenticationException; //この追加を忘れない use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array */ protected $dontFlash = [ 'password', 'password_confirmation', ]; /** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ //以下追加 public function unauthenticated($request, AuthenticationException $exception) { if($request->expectsJson()){ return response()->json(['message' => $exception->getMessage()], 401); } if (in_array('admin', $exception->guards())) { return redirect()->guest(route('admin.login')); } //return redirect()->action('HomeController@index'); //return redirect('/'); return redirect()->guest(route('login')); } // ここまで追加 }
■config/auth.php
<?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => 'user', // webからuserに変更した 'passwords' => 'users', ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'user' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, ], 'admins' => [ 'provider' => 'admins', 'table' => 'password_resets', 'expire' => 60, ], ], ];
どちらのコードをお見せすれば解決につながるかわかりませんでしたので、
他にこのコードを見せて、というものがあれば教えてください。
ログイン後のadmin情報の取得方法についてアドバイスいただけますと幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー