简体   繁体   English

从包含的 package 发布配置和迁移到 Laravel

[英]Publishing config and migrations from included package in Laravel

I'm using Laravel to build a package which will include some Admin functionality and will include some other packages.我正在使用 Laravel 构建一个 package,它将包含一些管理功能,并将包含一些其他包。

I have included a package which has migrations and a config file, But how do I copy those to the correct folder in Laravel. The package has it's own ServiceProvider but How do I call that from my package?我已经包含了一个 package,它有迁移和一个配置文件,但是我如何将它们复制到 Laravel 中的正确文件夹中。package 有它自己的 ServiceProvider,但是我如何从我的 package 中调用它?

I'm using the following to register the package in my package.我正在使用以下内容在我的 package 中注册 package。

class CldServiceProvider extends ServiceProvider {

    public function register()
    {
       $this->app->register(MediaLibraryServiceProvider::class);
    }
}

I've tried我试过了

php artisan vendor:publish 

But it says there's nothing to publish.但它说没有什么可发布的。 Because it's only looking at the packages for this Laravel installation, and not the nested packages.因为它只查看这个 Laravel 安装的包,而不是嵌套的包。

尝试单独发布配置并使用强制开关

php artisan vendor:publish --tag=config --force

As @scrubmx mentioned, you need to include the code that defines what can be published, though this code shouldn't really be in your own service provider, but rather the other package you're including. 正如@scrubmx所提到的,您需要包括定义可以发布内容的代码,尽管该代码不应真正包含在您自己的服务提供程序中,而应包含在您所包含的其他程序包中。 If it doesn't seem to have this code, it's best to list it as an issue on that package's repository or create a pull request to add it. 如果它似乎没有此代码,则最好将其列为该软件包存储库中的问题,或创建一个添加它的拉取请求。

Create database/migrations folder in your package root folder.在 package 根文件夹中创建database/migrations文件夹。 Then add publish details in boot method in your service provider.然后在您的服务提供商的启动方法中添加发布详细信息。

final class MyPackageServiceProvider extends BaseServiceProvider
{
    public function boot(): void
    {
        $this->publishes([
            __DIR__ . '/../database/migrations/' => database_path('migrations/my-package'),
        ], 'my-package-migrations');
    }
}

Second part could be like database_path('migrations') or database_path('migrations/my-package') .第二部分可能类似于database_path('migrations')database_path('migrations/my-package') I advise using with subfolder because if there are many packages migrations folder grows too much.我建议使用子文件夹,因为如果有很多包, migrations文件夹会增长太多。

The part of 'my-package-migrations' is our tag. 'my-package-migrations'部分是我们的标签。 And run publish command with tag.并运行带有标签的发布命令。

php artisan vendor:publish --tag=my-package-migrations

If you are using the subfolder php artisan migrate command won't work because we need to specify to exact migration folder like this,如果您使用子文件夹php artisan migrate命令将不起作用,因为我们需要像这样指定到确切的迁移文件夹,

php artisan migrate --path=/database/migrations/my-package

Yes, this looks a little bit dirty but organizes cables.是的,这看起来有点脏,但可以整理电缆。 And don't forget the using composer dump-autoload .并且不要忘记使用composer dump-autoload If the service provider is registered this solution works.如果服务提供商已注册,则此解决方案有效。 Also, you can use this method for config but config folder does not support subfolders.此外,您可以将此方法用于配置,但配置文件夹不支持子文件夹。

I think you have publish the config files yourself 我认为您已经自己发布了配置文件

$this->publishes([
    'other-package/absolute/path/some-config.php' => config_path('my-package.php'),
]);

Solution

I included the /packages/ folder in my current project where I was working on. 我在正在处理的当前项目中包含了/ packages /文件夹。 The problem is that when including the package in my composer.json 问题是当在我的composer.json中包含该软件包时

It is now reachable from 2 places inside my project, in the vendor folder and in my packages folder. 现在可以从我的项目中的2个位置(供应商文件夹和package文件夹中)访问它。 This was causing issue's 这是造成问题的

I moved my packages outside my current project, this fixed the problem. 我将软件包移到当前项目之外,这解决了该问题。

In my case I found I needed to include MY package in the application I was working on. 就我而言,我发现我需要在正在处理的应用程序中包括我的软件包。 This step was not included in the original tutorial I followed. 我遵循的原始教程未包含此步骤。

So in composer.json in the main app: 因此,在主应用程序的composer.json中:

"repositories": [
    {
        "type": "path",
        "url": "packages/namespace/name",
        "options": {
            "symlink": true
        }
    }
],
"require": {
    // ...
    "namespace/name": "dev-master"
},

And then we run this command from main folder: 然后我们从主文件夹运行以下命令:

composer update

After that running php artisan vendor:publish will include the option to publish the sub packages. 之后,运行php artisan vendor:publish将包括发布子软件包的选项。

NB: The source for this info is: https://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/ 注意:此信息的来源是: https : //laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/

Publish vendor migration : 发布供应商迁移:

php artisan vendor:publish --tag=migration

Publish vendor config : 发布供应商配置:

php artisan vendor:publish --tag=config

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

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