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

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

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

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

Laravel 5

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

Q&A

0回答

690閲覧

Laravel5.5 マルチ認証にてAdminの情報を取得したい

mikeko0901

総合スコア227

PHP

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

Laravel 5

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

0グッド

0クリップ

投稿2020/11/18 23:37

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情報の取得方法についてアドバイスいただけますと幸いです。

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

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

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

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

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

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

kawax

2020/11/19 00:16

5.5はすでにサポート期間切れなので答えないことにしている。なぜかマルチ認証使う人が多いけど認可機能があるLaravelではマルチ認証使う必要はほとんどない。世の中にはLaravelのこととか全く分かってない初心者が分かってるふりして間違った記事書いてることが多すぎるので騙されてはいけない。
mikeko0901

2020/11/20 06:19

5.5はやはり古いですか… 6系がよいでしょうか。。 マルチ認証なしでも実装できることは知りませんでした!ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問