简体   繁体   English

Laravel Auth Middleware:Class不存在

[英]Laravel Auth Middleware: Class can does not exist

I'm trying to protect a route via middleware as described in the doc 我正试图通过中间件保护路由,如文档中所述

When I hit the url, I get: 当我点击网址时,我得到:

ReflectionException in Container.php line 749:
Class can does not exist

Here's the relevant part from routes.php : 这是routes.php的相关部分:

Route::get('{user}/profile/edit/{type?}', [
    'as'   => 'edit',
    'uses' => 'User\UserController@edit',
    'middleware' => ['can:edit-user,user'],
]);

AuthServiceProvider.php : AuthServiceProvider.php

public function boot()
{
    $this->registerPolicies();

    // ... other definitions

    Gate::define('edit-user', function ($user, $subjectUser) {
        return
            $user->hasRole('manage.user') // if the user has this role, green
            ||
            ($user->isAdmin && $user->id == $subjectUser->id) // otherwise if is admin, can edit her own profile
            ;
    });

Maybe it's because I'm not using a separate policy class for defining the gate? 也许是因为我没有使用单独的策略类来定义门?

According to the documentation on using Middleware with Routes - you need to register the definition in app/Http/Kernel.php 根据使用带有路由的中间件文档 - 您需要在app/Http/Kernel.php注册定义

If you want a middleware to run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class. 如果您希望在对应用程序的每个HTTP请求期间运行中间件,只需在app / Http / Kernel.php类的$ middleware属性中列出中间件类。

The error you're seeing shows that this definition is missing. 您看到的错误表明此定义缺失。 You need to add something like; 你需要添加类似的东西;

// Within App\Http\Kernel Class...

protected $routeMiddleware = [
    //...
    'can' => \Path\To\Your\Middleware::class,
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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