laravel 自定义config
时间: 2023-07-11 14:33:59 浏览: 53
要自定义 Laravel 的配置文件,可以按照以下步骤进行操作:
1. 在 `config` 目录下创建一个新的配置文件,例如 `custom.php`。
2. 在 `custom.php` 文件中定义你需要自定义的配置项,例如:
```php
return [
'custom_setting' => 'custom_value',
];
```
3. 在 `config/app.php` 文件中注册你的自定义配置文件,例如:
```php
'providers' => [
// ...
App\Providers\CustomConfigServiceProvider::class,
],
```
4. 创建一个新的服务提供者 `CustomConfigServiceProvider`,并在 `register()` 方法中加载你的自定义配置文件,例如:
```php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
class CustomConfigServiceProvider extends ServiceProvider
{
public function register()
{
Config::set('custom', require __DIR__.'/../config/custom.php');
}
}
```
现在,你就可以在应用程序的任何地方使用 `config('custom.custom_setting')` 来访问你的自定义配置项了。