尝试发布 Laravel 包时无法找到可发布的资源

Unable to locate publishable resources when trying to publish a Laravel Package

我的目录设置如下所示:

|- packages
|-|-- Iezon
|---|-- Core
      |-- Admin
        |-- src
        |--|-- database
        |-----|-- migrations
        |--------|-- xxxx_xx_xx_xxxxxx_create_admin_panels.php
        |--|-- Http
        |----|-- routes.php
        |--|-- Providers
        |-----|-- AdminServiceProvider.php
        |--|-- Resources
        |-----|-- views

我的 AdminServiceProvider.php 看起来像这样:

namespace Iezon\Core\Admin\Providers;

use Illuminate\Support\ServiceProvider;

class AdminServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
        $this->loadViewsFrom (__DIR__ . '/../Resources/views', 'admin');
    }

    public function register()
    {
        $this->publishes([__DIR__ . '/../database/migrations', database_path('migrations')]);
    }

    protected function loadRoutesFrom(string $path)
    {
        require_once $path;
    }
}

我的 composer.json 看起来像这样:

"autoload": {
    "psr-4": {
        ...
        "Iezon\Core\Admin\": "packages/Iezon/Core/Admin/src",
        ...
    }
}

当我运行composer dumpautoload然后尝试运行:

php artisan vendor:publish --provider="Iezon\Core\Admin\Providers\AdminServiceProvider"

我明白了:

Unable to locate publishable resources.

然后我用 php artisan vendor:publish 检查它是否存在,但它没有显示在提供商列表中。我错过了什么?

$this->publishes(...) 实际上属于 boot 方法,而不属于 register。此外,您需要传递一个关联数组,但您传递的是两个单独的值。最后,最佳做法是仅在提供程序在控制台中实际为 运行 时才调用 publishes,如下所示:

if ($this->app->runningInConsole()) {
       $this->publishes([__DIR__ . '/../database/migrations' => database_path('migrations')]);
}