Self-developed a package, which uses provider to register the service, but it is useful to the Config () method to get the configuration, the problem is that the config file has not been publish, I feel that my thinking is not right ah.
The code is as follows:
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'), ]); }}
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 and then bind automaticly $models = Array_keys (config (' superview.models ')); foreach ($models as $model) {$this->app->singleton (' 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) {Retu RN config (' Superview.model_prefix '). $value; }, Array_keys (config (' superview.models '))); }}
Then I run: PHP artisan vendor:publish--provider= "Superviewproviderssuperviewconfigprovider"
You will get an error saying that the config content does not exist.
Reply content:
Self-developed a package, which uses provider to register the service, but it is useful to the Config () method to get the configuration, the problem is that the config file has not been publish, I feel that my thinking is not right ah.
The code is as follows:
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'), ]); }}
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 and then bind automaticly $models = Array_keys (config (' superview.models ')); foreach ($models as $model) {$this->app->singleton (' 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) {Retu RN config (' Superview.model_prefix '). $value; }, Array_keys (config (' superview.models '))); }}
Then I run: PHP artisan vendor:publish--provider= "Superviewproviderssuperviewconfigprovider"
You will get an error saying that the config content does not exist.
In the provider register
method, the first call is mergeConfigFrom
:
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'superview');
It means to base the configuration file in your package, then merge the configuration under the user's app directory, and finally get your superview configuration, and set it in the current config store.
You're going to write ServiceProvider
it. Don't you check to see if this file exists?
__DIR__.'/../../config/config.php
I'm not sure if I can use config here before I release config.
public function provides(){ return array_map(function($value) { return config('superview.model_prefix') . $value; }, array_keys(config('superview.models')));}