dominservice/laravel-config 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dominservice/laravel-config

最新稳定版本:2.3.2

Composer 安装命令:

composer require dominservice/laravel-config

包简介

Merge default config with dynamic settings, and optimize.

README 文档

README

Packagist Latest Version Total Downloads Software License

Merge default config with dynamic DB settings, include custom config files, and cache the result. Laravel 10-13 on PHP 8.1+.

Features

  • DB-backed config overrides without changing your config file structure.
  • Config cache builder that merges defaults + custom files + DB settings.
  • Custom config file paths via config/optimize.php (module-like layouts).
  • optimize_config() helper for .env replacement and ${VAR} interpolation.
  • Auto-generate optimize_config.php from .env on vendor:publish.
  • CLI command and middleware for on-demand config caching.
  • Route cache integration (command always, middleware in production).

Installation

composer require dominservice/laravel-config

Current compatibility targets:

  • Laravel 10 on PHP 8.1+
  • Laravel 11 on PHP 8.2+
  • Laravel 12 on PHP 8.2+
  • Laravel 13 on PHP 8.3+

Laravel package discovery is enabled. Manual registration is only needed if you disabled discovery:

'providers' => [
    // ...
    Dominservice\LaravelConfig\ServiceProvider::class,
],

Publish assets (config + migration):

php artisan vendor:publish --provider="Dominservice\\LaravelConfig\\ServiceProvider" --tag=optimize-config
php artisan vendor:publish --provider="Dominservice\\LaravelConfig\\ServiceProvider" --tag=optimize-migrations

Run migrations:

php artisan migrate

Usage

From shell:

php artisan dso:optimize-config

From PHP:

use Dominservice\LaravelConfig\Config;

(new Config())->buildCache();

Runtime refresh after buildCache()

After buildCache() the package immediately reloads the freshly written cached config file into the current Laravel runtime. This is meant for flows where settings are saved from an admin panel and the same request should already render with updated config() values.

The refresh works in three steps:

  • clear PHP stat cache for the cached config file
  • invalidate OPcache for that file when available
  • replace the in-memory Laravel config repository with a fresh Illuminate\\Config\\Repository

This matches Laravel's configuration bootstrap more closely than mutating the existing repository with app('config')->set([...]), which can leave mixed old/new runtime state in the same request.

Database-backed config values

The settings table stores only the values that differ from defaults. Types are auto-cast based on the default config value. If a value equals the default, the DB record is removed.

use Dominservice\LaravelConfig\Config;

(new Config())->set('app.name', 'My App', true);

(new Config())->set([
    'app.debug' => false,
    'app.timezone' => 'UTC',
], true);

(new Config())->set([
    ['app.locale', 'en'],
    ['app.faker_locale', 'en_US'],
], true);

Middleware (build cache on first request)

// app/Http/Kernel.php
protected $middlewareGroups = [
    'web' => [
        // ...
        Dominservice\LaravelConfig\Http\Middleware\Optimize::class,
    ],
];

The middleware runs only for GET requests, skips JSON and Livewire requests, and redirects once after building the cache. In production it also runs route:cache.

Custom config files

Use config/optimize.php to merge non-standard config files (for module systems, etc). Each key becomes the config namespace; values can be a string path or an array of paths.

// config/optimize.php
return [
    'custom_files_config' => [
        'module' => 'modules/module/config/module.php',
        'payments' => [
            'modules/payments/config/gateways.php',
            'modules/payments/config/rules.php',
        ],
    ],
];

The package merges these arrays recursively using Arr::recursiveMerge.

optimize_config helper (replace env())

optimize_config() reads from optimize_config.php in the project root and falls back to Env::get. It supports ${VAR} interpolation inside values.

Example in config/app.php:

'env' => optimize_config('APP_ENV', 'production'),

Auto-generate optimize_config.php

When you run vendor:publish, the service provider will:

  • Create optimize_config.php if it does not exist and .env exists.
  • Copy .env to .env.backup.
  • Remove the original .env.

Review this behavior before running vendor:publish in production.

Support

Support this project (Ko-fi)

If this package saves you time, consider buying me a coffee: https://ko-fi.com/dominservice - thank you!

License

MIT (c) Dominservice

统计信息

  • 总下载量: 84
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固