Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel add custom middleware to route group

in my web application i have admin panel and i'm trying to make access to users that they have admin role by this code:

namespace App\Http\Middleware;
use Closure;
class CheckUserAdminRole
{
    public function handle($request, Closure $next)
    {
        if (auth()->check()) {
            if (auth()->check() && !auth()->user()->hasRole('admin')) {
                auth()->logout();
                return redirect(route('system.messages','userIsNotAdmin'));
            }
        }
        return $next($request);
    }
}

and in my routs i have this route group:

Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web'], 'prefix' => 'dashboard'], function () {
    $this->group(['prefix' => 'administrator'], function () {
        $this->get('panel', 'AdminController@index');
});

my kernel:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    ...
    \App\Http\Middleware\CheckUserAdminRole::class,
];

now when i add my middleware as CheckUserAdminRole to route group like with this code:

Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','CheckUserAdminRole'], 'prefix' => 'dashboard'], function () {

i get this error:

Class CheckUserAdminRole does not exist

this codes couldn't resolve my problem:

php artisan route:clear
php artisan cache:clear
php artisan config:clear
composer dump-autoload
like image 248
DolDurma Avatar asked May 19 '18 16:05

DolDurma


3 Answers

Instead of registering your middleware in the $middleware array, you should register it in $routeMiddleware like so:

protected $routeMiddleware = [
    ...
    'checkAdmin' => \App\Http\Middleware\CheckUserAdminRole::class,
];

Note: registering a middlware in the $middleware array results in it being executed for every request and therefore is not applicable on specific routes.

Then you can use it in your routes with the checkAdmin name:

Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web','checkAdmin'], 'prefix' => 'dashboard'], function () {

Source

like image 199
SystemGlitch Avatar answered Oct 30 '22 11:10

SystemGlitch


You can also use middleware and route group together easily like so:

Route::group(['prefix' => 'admin',  'middleware' => 'auth'], function()
{
    //All the routes that belongs to the group goes here
    Route::get('dashboard', function() {} );
});
like image 22
Emeka Augustine Avatar answered Oct 30 '22 13:10

Emeka Augustine


Here's a simple fix without touching the Kernel file and taking advantage of the Policy. The accessAdmin is a function created inside Policy file.

Route::group(['namespace' => 'Dashboard', 'middleware' => 'can:accessAdmin, App\User', 'prefix' => 'dashboard'], function () {
$this->group(['prefix' => 'administrator'], function () {
    $this->get('panel', 'AdminController@index');
});
like image 27
Mel Avatar answered Oct 30 '22 13:10

Mel