Home > Backend Development > PHP Tutorial > When using the helper method config in the Laravel development package, an error occurs when running vendor:publish

When using the helper method config in the Laravel development package, an error occurs when running vendor:publish

WBOY
Release: 2023-03-02 06:00:02
Original
1369 people have browsed it

I developed a Package myself, which uses Provider to register services, but the config() method is used to obtain the configuration. The problem is that the config file has not been published yet. I feel that my thinking is wrong.
The code is as follows:

<code>class SuperViewConfigProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Publish the config file to 
        $this->publishes([
            __DIR__.'/../../config/config.php' => config_path('superview.php'),
        ]);
    }
}</code>
Copy after login
Copy after login
<code>class SuperViewModelProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Get config, then bind automaticly
        $models = array_keys(config('superview.models'));
        foreach ($models as $model) {
            $this->app->singleton(config('superview.model_prefix') . $model, function($app) use ($models, $model) {
                return new $models[$model];
            });
        }
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
    }
}</code>
Copy after login
Copy after login

Then I run: php artisan vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
An error will be reported saying that the config content does not exist.

Reply content:

I developed a Package myself, which uses Provider to register services, but the config() method is used to obtain the configuration. The problem is that the config file has not been published yet. I feel that my thinking is wrong.
The code is as follows:

<code>class SuperViewConfigProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Publish the config file to 
        $this->publishes([
            __DIR__.'/../../config/config.php' => config_path('superview.php'),
        ]);
    }
}</code>
Copy after login
Copy after login
<code>class SuperViewModelProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Get config, then bind automaticly
        $models = array_keys(config('superview.models'));
        foreach ($models as $model) {
            $this->app->singleton(config('superview.model_prefix') . $model, function($app) use ($models, $model) {
                return new $models[$model];
            });
        }
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
    }
}</code>
Copy after login
Copy after login

Then I run: php artisan vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
An error will be reported saying that the config content does not exist.

First call mergeConfigFrom in the provider's register method:

<code class="php">$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'superview');</code>
Copy after login

It means to use the configuration file in your package as the basis, and then merge the configuration in the user app directory, and finally get the configuration of your superview, and set it in the current Config storage.

You can write ServiceProvider, so why not check whether this file exists?

<code>__DIR__.'/../../config/config.php</code>
Copy after login

I’m not sure whether config can be used here before config is released

<code>public function provides()
{
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template