Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Properly Create a Laravel Package

I am having some issues with developing a package. I have read through several tutorials on the web and followed their advice but for some reason I still run into the same issue.

What I am trying to do: Develop a package in an active project

In my Laravel folder, I have a packages/companyname/laravel-model-notes. Inside that package, I have a composer.json file with the following:

{
    "name": "companyname/laravel-model-notes",
    "description": "A package to add notes to an eloquent model in Laravel.",
    "type": "library",
    "require": {},
    "config": {
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "CompanyName\LaravelModelNotes\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "CompanyName\LaravelModelNotes\Tests\": "tests"
        }
    },
    "extra": {
        "laravel": {
            "providers": [
                "CompanyName\LaravelModelNotes\NoteServiceProvider"
            ]
        }
    }
}

And then in my laravel composer.json file, I have added the following:

    "autoload": {
        "psr-4": {
            "App\": "app/",
            "CompanyName\LaravelModelNotes\": "packages/companyname/laravel-model-notes/src"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/Helpers/ThemeHelper.php",
            "app/Helpers/BaseHelper.php"
        ]
    },

When I manually add the service provider to the App.php things work, however artisan commands like publishing the migrations does not work. For some reason, Laravel does not see the package?

Is there some special way of building a package inside of a project and having that project recognize the package?

I am using Laravel 7.

Below is my service provider:

class NoteServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__.'/../config/config.php' => config_path('notes.php'),
            ], 'config');


            if (! class_exists('CreateNotesTable')) {

                // TODO: If tenant folder exists publish there too.
                $this->publishes([
                    __DIR__.'/../database/migrations/create_notes_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_notes_table.php'),
                ], 'migrations');
            }
        }
    }

    /**
     * Register the application services.
     */
    public function register()
    {
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'notes');
    }
}
question from:https://stackoverflow.com/questions/65835981/properly-create-a-laravel-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...