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
WBOYOriginal
2016-12-01 00:56:421374browse

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>
<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>

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>
<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>

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>

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>

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>
Statement:
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