如何在我自己的程序包中使用Laravel程序包?

时间:2019-07-29 13:57:24

标签: php laravel laravel-5 package composer-php

我正在Laravel中开发一个Role-Permission包,我想使用这个包;

Laravel permission 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 Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;

当我提供它时,显示此错误消息...

  

Symfony \组件\调试\异常\ FatalErrorException   (E_UNKNOWN)未发现特征'Spatie \ Permission \ Traits \ HasRoles'

我在这里做什么错了?

[1]: https://i.stack.imgur.com/KRUT0.png

1 个答案:

答案 0 :(得分:0)

问题是您只是将软件包文件自动加载到项目中。这样,项目的作曲者对软件包的依赖项一无所知(因此为什么未安装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中删除软件包源文件的手动自动加载(编写者将使用软件包的autoload部分将/src绑定到Module\Permission命名空间):

"psr-4": {
     "App\\": "app/",
     "Modul\\Permission\\": "packages/modul/permission/src" <--- remove this line
}

最后,运行composer require modul/permission。 Composer会在我们为其指定的路径存储库中找到它,并将packages/modul/permission目录符号链接到vendor/modul/permission并安装其依赖项。

现在,您可以在packages/modul/permission文件夹中编辑软件包。完成后,请确保将其在线发布在github / packagist上,以便可以从packagist存储库中向所有人远程访问它,而不仅仅是从本地路径访问。