laravel扩展包的配置文件复制到config文件夹下的问题解决
Jun 20, 2016 pm 12:29 PM
我们在写扩展包的时候,总是会纠结,里面的一些个性化的配置要怎么处理呢。
laravel 很好的解决了这个问题。
laravel 的 ServiceProvider很好的解决了这个问题。
具体来说,如果,你想要在外部配置你的扩展包,那么,就先要把你这个 包注册成 服务。
首先需要定义一个类 继承 ServiceProvider.
1.里面定义一个register函数 指定 包所用的配置文件的绝对路径,然后把配置的外部文件和内部文件的内容合并。
2.在boot函数中,指定 publish的具体位置。
代码如下
<?php/** * Created by Bane.Shi. * Copyright MoenSun * User: Bane.Shi * Date: 16/4/30 * Time: 00:06 */namespace MoenSun\MSFileSystem;use Illuminate\Support\ServiceProvider;class MSFileSystemServiceProvider extends ServiceProvider{ public function register() { $configPath = __DIR__ . '/../../../config/mslaravelsystem.php'; $this->mergeConfigFrom($configPath,"mslaravelsystem"); } public function boot(){ $app = $this->app; $configPath = __DIR__ . '/../../../config/mslaravelsystem.php'; $this->publishes([$configPath => $this->getConfigPath()],'config'); } public function getConfigPath(){ return config_path("mslaravelsystem.php"); } protected function publishConfig($configPath) { $this->publishes([$configPath => config_path('mslaravelsystem.php')], 'config'); }}
在 config/app.php 文件夹下,添加该服务
在 providers 数组下添加
MoenSun\MSFileSystem\MSFileSystemServiceProvider::class
再执行
php artisan vendor:publish
这样文件就会copy 到config文件夹下了。

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium)

Arbeiten mit Flash -Sitzungsdaten in Laravel

Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren

Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs

12 Beste PHP -Chat -Skripte auf Codecanyon
