承接 dominservice/laravel-config 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dominservice/laravel-config

最新稳定版本:2.2.4

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+ 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

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();

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

统计信息

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

GitHub 信息

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

其他信息

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