我們在寫擴充包的時候,總是會糾結,裡面的一些個人化的配置要怎麼處理呢。
laravel 很好的解決了這個問題。
laravel 的 ServiceProvider很好的解決了這個問題。
具體來說,如果,你想要在外部配置你的擴充包,那麼,就先要把你這個 包註冊成 服務。
首先需要定義一個類 繼承 ServiceProvider.
1.裡面定義一個register函數 指定 包所用的設定檔的絕對路徑,然後把配置的外部檔案和內部檔案的內容合并。
2.在boot函數中,指定 publish的具體位置。
代碼如下
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檔案夾下了。