爱问知识人 爱问教育 医院库

Laravel开发package里使用helper方法config的时候,运行vendor:publish报错

首页

Laravel开发package里使用helper方法config的时候,运行vendor:publish报错


        

提交回答
好评回答
  • 2023-03-13 17:30:00
      自己开发了一个Package,其中使用Provider来注册服务,但是里面有用到config()方法来获取配置,问题是这个config文件还没有被publish,感觉我的思路不对啊。
    代码如下: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, 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'))); }}然后我运行: php artisan vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
    就会报错了说config内容不存在。
      自己开发了一个Package,其中使用Provider来注册服务,但是里面有用到config()方法来获取配置,问题是这个config文件还没有被publish,感觉我的思路不对啊。
    代码如下: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, 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'))); }}然后我运行: php artisan vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
    就会报错了说config内容不存在。
      在 provider 的 register 方法里首先调用 mergeConfigFrom :$this->mergeConfigFrom(__DIR__。'/。/。/config/config。php', 'superview');意思是以你包里面的配置文件作为基础,然后合并用户app目录下的配置,最终得到你的 superview 的配置,并且设置在当前的Config存储中。
      你都会写 ServiceProvider了 那难道不检查这个文件是否存在?__DIR__。'/。/。/config/config。php我不确定,在没发布config前,这里能否用到configpublic function provides(){ return array_map(function($value) { return config('superview。
      model_prefix') 。 $value; }, array_keys(config('superview。models')));}。

    可***

    2023-03-13 17:30:00

类似问题

换一换

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):