簡體   English   中英

Laravel Auth Middleware:Class不存在

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

我正試圖通過中間件保護路由,如文檔中所述

當我點擊網址時,我得到:

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

這是routes.php的相關部分:

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

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

也許是因為我沒有使用單獨的策略類來定義門?

根據使用帶有路由的中間件文檔 - 您需要在app/Http/Kernel.php注冊定義

如果您希望在對應用程序的每個HTTP請求期間運行中間件,只需在app / Http / Kernel.php類的$ middleware屬性中列出中間件類。

您看到的錯誤表明此定義缺失。 你需要添加類似的東西;

// 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