Home > Article > Backend Development > When using the helper method config in the Laravel development package, an error occurs when running vendor:publish
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.
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>