【问题标题】:use package's config files in laravel 5在 laravel 5 中使用包的配置文件
【发布时间】:2015-06-28 22:41:01
【问题描述】:

我有一个基于 laravel 5 包的应用程序。在包中这是我的结构:

Packages
       -vendor
             -xxx
                 -public
                 -src
                     -config
                           -config.php
                     -Vendor
                          -Xxx
                             -Controllers
                             -Models
                             -Repositories
                             XxxServiceProvider.php
                 -views
                 routes.php

**以上是我的包结构:xxx是包名。

在这个 config/config.php 我有我在这个包中使用的配置。

如何访问此配置文件以使用其中的值?

我在 XxxServiceProvider.php 中添加了如下代码

$this->publishes([   __DIR__.'/xxx/src/config/config.php'=>config_path('xxx/config.php'),
             ]);

和 php artisan vendor:publish ,它给出了以下错误: 找不到路径:

谁能帮我解决这个问题?

【问题讨论】:

  • 你的包结构不清楚。可能您的包配置路径是引发该错误的路径。检查指定的路径是否正确。您可以使用Config::get('package::file.option'); 访问您的包配置,或者如果您已发布,那么您可以像Config::get('file.option'); 一样访问它

标签: laravel package config


【解决方案1】:

如果您的包结构如下所示,那么您可以像这样加载包的配置文件。尝试根据您自己的结构对其进行自定义。

/xxx
    |_ /src
        |_ /Config
            - xxx.php
        - XxxServiceProvider.php

添加到XxxServiceProvider.php

public function boot() {

    // Allow your user to publish the config
    $this->publishes([
        __DIR__.'/Config/xxx.php' => config_path('xxx.php'),
    ], 'config');

}


public function register() {

    // Load the config file and merge it with the user's (should it get published)
    $this->mergeConfigFrom( __DIR__.'/Config/xxx.php', 'xxx');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多