简体   繁体   中英

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

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

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() {} );
});

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');
});

You can try middleware with prefix and groups.

Route::middleware(['Auth'])->prefix('api/')->group(function() {
    Route::group(['prefix' => 'review/'], function () {
       Route::get('/', 'User\Controllers\Api\UserController@getUserReviews');
    });
});

Hope its helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM