hamoda-dev/configure

该包最新版本(1.0)没有可用的许可证信息。

配置 PHP 库

1.0 2020-08-03 07:30 UTC

This package is auto-updated.

Last update: 2024-09-26 22:45:19 UTC


README

PHP 配置库。用于从文件中获取所有应用配置(开发、生产等环境)。使用简单,只需几个步骤即可完成。

安装

通过 composer 安装

composer require hamoda-dev/configure

配置

1 - 将以下内容复制到你的 composer.json 文件中

"scripts": {
        "configure-init": [
            "cp -r -d vendor/hamoda-dev/configure/sample/config config",
            "cp -r vendor/hamoda-dev/configure/sample/configure.json configure.json"
        ]
}

2 - 在你的项目中运行以下命令以创建配置目录和 configure.json

    composer run-script configure-init

现在你有配置目录和 configure.json

配置目录

包含目录 {开发,生产}

  • 在开发目录中放置你的开发配置文件
  • 在生产目录中放置你的生产配置文件

你可以在 configure.json 文件中切换环境

configure.json
{
  "config_path" : "config",
  "environment" : "development"
}

"environment" : "here you switching environment" => developmentproduction

语法

<?php

    return [
        'app_url' => 'https://',
        'mysql' => [
            'driver' => 'mysql',
            'url' => '',
            'host' => '127.0.0.1',
            'port' => '3306',
            'database' => 'forge',
            'username' => 'root',
            'password' => '',
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ]

用法

你可以通过 config($parm) 访问所有配置。接受一个参数 = 需要的配置

从上一个语法示例 config('app_url') 输出应为: https:// 如果你需要更深层级的值,只需使用 . 在键之间 config('mysql.host') 输出应为: 127.0.0.1

你可以添加任意数量的配置文件,并且可以非常容易地访问它们

享受吧 :)