我正在在Laravel中开发一个角色接触软件包,我想使用此软件包;
laravel许可github
问题是,当我在自己的软件包中安装此软件包时,我无法在主项目中使用某些功能。示例" hasroles"
我的软件包composer.json文件
"require": {
"spatie/laravel-permission": "dev-master"
},
"autoload": {
"psr-4": {
"Modul\Permission\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Spatie\Permission\PermissionServiceProvider"
]
}
}
主要项目作曲家文件
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^7.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\": "app/",
"Modul\Permission\": "packages/modul/permission/src"
},
"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"
],
"post-root-package-install": [
"@php -r "file_exists('.env') || copy('.env.example', '.env');""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
和我的用户模型;
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use SpatiePermissionTraitsHasRoles;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
当我提供其显示此错误消息时...
symfony component debug exception fatalerrorexception (e_unknown(特质'spatie 允许特质 hasroles'找不到
我在这里做错了什么?
[1]:https://i.stack.imgur.com/krut0.png
问题是您只是在将软件包文件自动加载到项目中。这样,您项目的作曲家对您的包裹的依赖项一无所知(因此,为什么未安装spatie/permission
软件包(。
正确的方法是需要您的软件包进入您的项目。通常,您会为项目创建一个存储库,请在https://packagist.org上注册为modul/permission
,然后为您的项目运行composer require modul/permission
。
但是,如果您的软件包仍未完全开发,我建议您不从Packagist,而是来自所谓的path repository
。在您项目的composer.json
中添加以下部分:
{
...
"repositories": [
{
"type": "path",
"url": "packages/modul/permission"
},
]
...
}
当您需要时,这将使作曲家查看您的packages/modul/permission
目录。因此,请从项目的composer.json
中删除包源文件的手册自动加载(作曲家将使用您的软件包的自动加载部分将/src
绑定到ModulePermission
名称空间(:
"psr-4": {
"App\": "app/",
"Modul\Permission\": "packages/modul/permission/src" <--- remove this line
}
最后,运行composer require modul/permission
。作曲家将在我们为他指定的路径存储库中找到它,并将packages/modul/permission
目录与vendor/modul/permission
并安装依赖关系。
现在,您可以在packages/modul/permission
文件夹中编辑软件包。完成后,请务必在GitHub/Packagist上在线发布它,以便可以从Packagist存储库中远程提供给所有人,而不仅仅是从您的本地路径上使用。