システム管理者用のダッシュボード /admin/dashboard と教員用のダッシュボード /professor/dashboard を作成します.まずは,認証不要で誰でも閲覧できるようにして,その後に認証を求めるようにしていきます.
システム管理者用と教員用のコントローラに index 関数を作成します.
app/Http/Controllers/Admin/DashboardController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
dd("admin dashboard");
}
}
app/Http/Controllers/Professor/DashboardController.php
<?php
namespace App\Http\Controllers\Professor;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
dd("professor dashboard");
}
}
次に,routes ディレクトリに admin.php と professor.php を作成し,それぞれのルートを定義します.いずれも prefix
を指定していることに注意して下さい.したがって,Admin の場合は,ルートを /dashboard と定義していますが,実際には /admin/dashboard になり,Professor の場合は,/professor/dashboard になります.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController;
// Admin 認証不要
Route::group(['prefix' => 'admin'], function() {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
});
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Professor\DashboardController;
// Professor 認証不要
Route::group(['prefix' => 'professor'], function() {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('professor.dashboard');
});
いま作成した2つのルート定義ファイルを web.php で読み込むようにします.
<?php
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\User\DashboardController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
// Route::get('/dashboard', function () {
// return view('dashboard');
// })->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
Route::middleware('auth', 'verified')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});
require __DIR__.'/auth.php';
require __DIR__.'/admin.php';
require __DIR__.'/professor.php';
ルートが定義されているか確認します.
vagrant@ubuntu2204 laravelAuth $ php artisan route:list GET|HEAD / ....................................................... POST _ignition/execute-solution ignition.executeSolution › Sp… GET|HEAD _ignition/health-check ignition.healthCheck › Spatie\Lar… POST _ignition/update-config ignition.updateConfig › Spatie\L… GET|HEAD admin/dashboard admin.dashboard › Admin\DashboardControl… GET|HEAD api/user ................................................ GET|HEAD confirm-password password.confirm › Auth\ConfirmablePass… POST confirm-password Auth\ConfirmablePasswordController@store GET|HEAD dashboard .... dashboard › User\DashboardController@index POST email/verification-notification ....... verification.send GET|HEAD email/verify ........................ verification.notice GET|HEAD email/verify/{id}/{hash} ............ verification.verify GET|HEAD forgot-password password.request › Auth\PasswordResetLin… POST forgot-password password.email › Auth\PasswordResetLinkC… GET|HEAD login login › Auth\AuthenticatedSessionController@create POST login ......... Auth\AuthenticatedSessionController@store POST logout logout › Auth\AuthenticatedSessionController@dest… PUT password password.update › Auth\PasswordController@update GET|HEAD professor/dashboard professor.dashboard › Professor\Dash… GET|HEAD profile ........... profile.edit › ProfileController@edit PATCH profile ....... profile.update › ProfileController@update DELETE profile ..... profile.destroy › ProfileController@destroy GET|HEAD register register › Auth\RegisteredUserController@create POST register ............ Auth\RegisteredUserController@store POST reset-password password.store › Auth\NewPasswordControll… GET|HEAD reset-password/{token} password.reset › Auth\NewPassword… GET|HEAD sanctum/csrf-cookie sanctum.csrf-cookie › Laravel\Sanctu… GET|HEAD verify-email verification.notice › Auth\EmailVerificatio… GET|HEAD verify-email/{id}/{hash} verification.verify › Auth\Veri… Showing [29] routes vagrant@ubuntu2204 laravelAuth $
ルートの定義を変更したので Web サーバを再起動します.
vagrant@ubuntu2204 laravelAuth $ php artisan serve --host=192.168.56.101 --port=8000 ⏎
INFO Server running on [http://192.168.56.101:8000].
Press Ctrl+C to stop the server
管理者のダッシュボードページ /admin/dashboard にアクセスします.
教員のダッシュボードページ /professor/dashboard にアクセスします.