我已经在我的Laravel 5.1应用程序中安装了Entrust软件包,方法是将它添加到我的composer.json文件并运行composer update并按照所有说明进行操作.我试图使用他们的中间件来保护一些路由,但我得到以下异常:
ReflectionException in Container.php line 741: Class App\Http\Zizaco\Entrust\Middleware\EntrustPermission does not exist
这是我的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" } }
以下是我的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', ]
这是我的App\Http\Kernel.php文件的内容:
\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, ]; }
以下是我在routes.php文件中尝试使用中间件的方法:
// secure admin routes Route::group(['middleware' => 'permission:access_admin_area'], function() { Route::get('admin', ['as' => 'dashboard', 'uses' => 'Admin\AdminController@index']); });
我很确定我已经按照他们的指示写了这封信,直到我尝试使用他们的路由中间件时才一直工作.谁知道我哪里出错了?
原来在安装说明中的代码是错误的.路由中间件定义需要在前面加上反斜杠.
破碎的例子:
/** * 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, ];
固定示例:
/** * 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, ];
感谢xaoc的抬头!