简体   繁体   English

在Laravel 5.1中使用Entrust中间件时出现异常

[英]Exception when using Entrust middleware in Laravel 5.1

I have installed the Entrust package in my Laravel 5.1 application by adding it to my composer.json file and running composer update and followed all of the instructions to the letter. 我已经在我的Laravel 5.1应用程序中安装了Entrust软件包,方法是将其添加到我的composer.json文件并运行composer update并按照所有说明进行操作。 I am trying to use their middleware to protect some routes but I get the following exception: 我试图使用他们的中间件来保护一些路由,但我得到以下异常:

ReflectionException in Container.php line 741:
Class App\Http\Zizaco\Entrust\Middleware\EntrustPermission does not exist

Here is the contents of my composer.json file: 这是我的composer.json文件的内容:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "LaravelCollective/html": "5.1",
        "zizaco/entrust": "dev-laravel-5",
        "yajra/laravel-datatables-oracle": "~5.0",
        "davejamesmiller/laravel-breadcrumbs": "~3.0",
        "intervention/image": "dev-master",
        "doctrine/dbal": "2.5.1",
        "thujohn/twitter": "~2.0",
        "fzaninotto/faker": "^1.5",
        "cviebrock/image-validator": "2.0.*@beta"
    },
    "require-dev": {
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database",
            "app/Http/Helpers"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

Here is the relevant content of my config/app.php file: 以下是我的config / app.php文件的相关内容:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Routing\ControllerServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    Collective\Html\HtmlServiceProvider::class,

    'yajra\Datatables\DatatablesServiceProvider', // data grid
    'DaveJamesMiller\Breadcrumbs\ServiceProvider',
    'Zizaco\Entrust\EntrustServiceProvider',
    'Intervention\Image\ImageServiceProvider',
    'Thujohn\Twitter\TwitterServiceProvider',
    'Cviebrock\ImageValidator\ImageValidatorServiceProvider'
],

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => [

    'App'       => Illuminate\Support\Facades\App::class,
    'Artisan'   => Illuminate\Support\Facades\Artisan::class,
    'Auth'      => Illuminate\Support\Facades\Auth::class,
    'Blade'     => Illuminate\Support\Facades\Blade::class,
    'Bus'       => Illuminate\Support\Facades\Bus::class,
    'Cache'     => Illuminate\Support\Facades\Cache::class,
    'Config'    => Illuminate\Support\Facades\Config::class,
    'Cookie'    => Illuminate\Support\Facades\Cookie::class,
    'Crypt'     => Illuminate\Support\Facades\Crypt::class,
    'DB'        => Illuminate\Support\Facades\DB::class,
    'Eloquent'  => Illuminate\Database\Eloquent\Model::class,
    'Event'     => Illuminate\Support\Facades\Event::class,
    'File'      => Illuminate\Support\Facades\File::class,
    'Hash'      => Illuminate\Support\Facades\Hash::class,
    'Input'     => Illuminate\Support\Facades\Input::class,
    'Inspiring' => Illuminate\Foundation\Inspiring::class,
    'Lang'      => Illuminate\Support\Facades\Lang::class,
    'Log'       => Illuminate\Support\Facades\Log::class,
    'Mail'      => Illuminate\Support\Facades\Mail::class,
    'Password'  => Illuminate\Support\Facades\Password::class,
    'Queue'     => Illuminate\Support\Facades\Queue::class,
    'Redirect'  => Illuminate\Support\Facades\Redirect::class,
    'Redis'     => Illuminate\Support\Facades\Redis::class,
    'Request'   => Illuminate\Support\Facades\Request::class,
    'Response'  => Illuminate\Support\Facades\Response::class,
    'Route'     => Illuminate\Support\Facades\Route::class,
    'Schema'    => Illuminate\Support\Facades\Schema::class,
    'Session'   => Illuminate\Support\Facades\Session::class,
    'Storage'   => Illuminate\Support\Facades\Storage::class,
    'URL'       => Illuminate\Support\Facades\URL::class,
    'Validator' => Illuminate\Support\Facades\Validator::class,
    'View'      => Illuminate\Support\Facades\View::class,

    'Breadcrumbs'   => 'DaveJamesMiller\Breadcrumbs\Facade',
    'Datatables' => yajra\Datatables\Datatables::class,
    Zizaco\Entrust\EntrustServiceProvider::class,
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
    'Image_helper' => 'App\Http\Helpers\Image_helper',
    'Twitter' => 'Thujohn\Twitter\Facades\Twitter',
]

Here is the content of my App\\Http\\Kernel.php file: 这是我的App \\ Http \\ Kernel.php文件的内容:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'clientIpCheck' => 'App\Http\Middleware\ClientIpCheck',
        'role' => Zizaco\Entrust\Middleware\EntrustRole::class,
        'permission' => Zizaco\Entrust\Middleware\EntrustPermission::class,
        'ability' => Zizaco\Entrust\Middleware\EntrustAbility::class,
    ];
}

And here is how I am trying to use the middleware in my routes.php file: 以下是我在routes.php文件中尝试使用中间件的方法:

// secure admin routes
Route::group(['middleware' => 'permission:access_admin_area'], function()
{
    Route::get('admin', ['as' => 'dashboard', 'uses' => 'Admin\AdminController@index']);
});

I'm pretty sure I've followed their instructions to the letter, it was all working until I tried to use their route middleware. 我很确定我已经按照他们的指示写了这封信,直到我尝试使用他们的路由中间件时才一直工作。 Anyone know where I've gone wrong? 谁知道我哪里出错了?

Turns out the code in the installation instructions is wrong. 原来在安装说明中的代码是错误的。 The route middleware definitions need a backslash on the front. 路由中间件定义需要在前面加上反斜杠。

Broken example: 破碎的例子:

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'clientIpCheck' => 'App\Http\Middleware\ClientIpCheck',
    'role' => Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => Zizaco\Entrust\Middleware\EntrustAbility::class,
];

Fixed example: 固定示例:

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'clientIpCheck' => 'App\Http\Middleware\ClientIpCheck',
    'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
];

Thanks to xaoc for the heads up! 感谢xaoc的抬头!

Check This Failed to Attach ENTRUST Middleware 检查此无法附加ENTRUST中间件

Or try to create your MiddleWares Manully 或者尝试创建您的MiddleWares Manully

1- Go to Zizaco\\Entrust\\Middleware in vendor directory 1-转到vendor目录中的Zizaco\\Entrust\\Middleware

2- Copy all MiddleWares from Middleware directory 2-从Middleware目录复制所有MiddleWares

3- Paste all in App\\Http\\Middleware and change the path in $routeMiddleware[] to your app middleware dir 3-在App\\Http\\Middleware粘贴所有内容并将$routeMiddleware[]的路径更改为您的app中间件目录

Note: don`t forget to change namespace after copy 注意:不要忘记在复制后更改命名空间

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

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