我正在尝试安装软件包spatie/laravel-permission
,以帮助管理我在应用程序中的角色和权限。我一直遇到以下问题。根据文档,要安装Laravel permission
,请运行以下命令
composer require spatie/laravel-permission
但是,当我这样做时,会出现此错误:
> @php artisan package:discover --ansi
ErrorException : Trying to access array offset on value of type null
at /home/anto/encomm/projects/vendor/spatie/laravel-permission/src/PermissionServiceProvider.php:61
57| protected function registerModelBindings()
58| {
59| $config = $this->app->config['permission.models'];
60|
> 61| $this->app->bind(PermissionContract::class, $config['permission']);
62| $this->app->bind(RoleContract::class, $config['role']);
63| }
64|
65| protected function registerBladeExtensions()
Exception trace:
1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError()
/home/anto/encomm/projects/vendor/spatie/laravel-permission/src/PermissionServiceProvider.php:61
2 Spatie\Permission\PermissionServiceProvider::registerModelBindings()
/home/anto/encomm/projects/vendor/spatie/laravel-permission/src/PermissionServiceProvider.php:36
Please use the argument -v to see more details.
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
Installation failed, reverting ./composer.json to its original content.
以下是我的composer.json
文件的内容:
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.4",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.2",
"laravel/tinker": "^2.0",
"laravel/ui": "^1.1",
"owen-it/laravel-auditing": "^9.3",
"sudiptpa/guid": "^1.0",
"unisharp/laravel-filemanager": "^2.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
答案 0 :(得分:1)
运行第一个vendor/spatie/laravel-permission/src/PermissionServiceProvider.php
语句后,您需要对文件composer require spatie/laravel-permission
进行一些更改
这是第61/62行的实际代码:
$this->app->bind(PermissionContract::class, $config['permission']);
$this->app->bind(PermissionContract::class, $config['role']);
需要对此进行更改:
$this->app->bind(PermissionContract::class, isset($config['permission']));
$this->app->bind(PermissionContract::class, isset($config['role']));
完成该更改后,再次运行composer require spatie/laravel-permission
,仅此而已。在最新版本的PHP中,您必须使用isset():