Laravel 向路由组添加自定义中间件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50427439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:43:53  来源:igfitidea点击:

Laravel add custom middleware to route group

laravellaravel-5.6

提问by DolDurma

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:

在我的 Web 应用程序中,我有管理面板,我正在尝试通过以下代码访问他们具有管理员角色的用户:

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 CheckUserAdminRoleto route group like with this code:

现在,当我CheckUserAdminRole使用以下代码将中间件添加到路由组时:

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

回答by SystemGlitch

Instead of registering your middleware in the $middlewarearray, you should register it in $routeMiddlewarelike so:

不要在$middleware数组中注册你的中间件,你应该$routeMiddleware像这样注册它:

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

Note: registering a middlware in the $middlewarearray results in it being executed for everyrequest and therefore is not applicable on specific routes.

注意:在$middleware数组中注册一个中间件会导致它为每个请求执行,因此不适用于特定路由。

Then you can use it in your routes with the checkAdminname:

然后你可以在你的路由中使用它的checkAdmin名字:

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

Source

来源

回答by Emeka Augustine

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

回答by Kaushik shrimali

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

希望它有帮助