I am trying to redirect the admin to dashboard page by the following codes but when I enter /dashboard
, the browser displays a NotFoundHttpException
error page.
Middleware (AdminCheck.php) :
<?php
namespace App\Http\Middleware;
use Closure;
class AdminCheck
{
public function handle($request, Closure $next)
{
$user = auth()->authenticate();
if ($user->role !== 'admin')
{
return redirect(route('login'));
}
return $next($request);
}
}
Kernel.php (App\Http\Kernel.php) :
protected $routeMiddleware = [
...
'adminCheck' => \App\Http\Middleware\AdminCheck::class,
];
Route (App\routes\web.php) :
Route::get('dashboard', function (){
//
})->middleware('auth', 'adminCheck');
dashboard.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class dashboard extends Controller
{
public function index ()
{
return view('dashboard');
}
}
When I enter http://localhost:8000/dashboard
there is an error page displayed that says :
Sorry, the page you are looking for could not be found.
I seem so dumb by not being able to fix it. Would you help me find out where my problem is? Thank you so much in advance.
Maybe it comes from your controller name. Laravel follows the PSR-4 standard for autoloading classes, see doc here, your class name should start by an uppercase :
\NamespaceName{\SubNamespaceNames*}\ClassName
Try to rename your dashboard.php into Dashboard.php, remake a php composer dumpautoload
to see ? This route should work after that :
Route::get('dashboard', 'Dashboard@index')->middleware('auth', 'adminCheck');