承接 prinx/dotenv 相关项目开发

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

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

prinx/dotenv

Composer 安装命令:

composer require prinx/dotenv

包简介

Make easy access to the environment variables in your application

README 文档

README

PHP Dotenv

tests codecov

Get easily access to environment variables.

Installation

Open a command prompt into your project root folder and run:

composer require prinx/dotenv

Usage

Quick start

// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';

/*
 * Retrieve an environment variable. Returns null if variable not found.
 */
$hostname = env('DEV_DB_HOST');

/*
 * Retrieve an environment variable. Returns default value passed as second argument if variable not found
 */
$port = env('DEV_DB_PORT', 3306);

/*
 * Add a variable to the current loaded environment (will not save in the .env file)
 */
addenv('LOG_LEVEL', 'info');

/*
 * Wrtie variable to the env file. 
 * Will also automatically load the variable into the current environment.
 * If the file already contains the variable, the variable will be overwritten.
 */
persistenv('LOG_LEVEL', 'warn');
persistenv('LOG_LEVEL', 'debug');
persistenv('LOG_LEVEL', 'info');

/*
 * Get all environment variables
 */
env()
// OR
allenv();

Writing a .env file

The .env file format will be:

VARIABLE_NAME=value

For example:

SESSION_DRIVER=file
DEV_DB_HOST=localhost
DEV_DB_PORT=3306

PROD_DB_HOST=prod_db_ip
PROD_DB_PORT=3308 

As standard, the variable name is capital letter with underscores to separate words.

Comments

You can write comments in your .env file by preceding the comment by a hash (#). Example:

# Supported: file|database
SESSION_DRIVER=file

Types of values

By default, env variable will be retrieved as string, except booleans, and null.

String

You can use quotes to surround strings.

APP_NAME=My app
# or
APP_NAME="My app"

DB_HOST=173.0.0.0
# or
DB_HOST="173.0.0.0"
Boolean

The values true, "true" or 'true', will be got as the boolean true. The values false, "false" or 'false' will be got as the boolean false.

# Will be got as a boolean true
APP_DEBUG=true

# Will be got as a boolean false
APP_DEBUG=false

Same as:

APP_DEBUG="true"

APP_DEBUG="false"
Null

The values null, "null" or 'null', will be got as null.

# Will be got as a null
APP_DEBUG=null

APP_DEBUG="null"

Referring to another variable

You can refer to the value of another variable in your .env file by putting the name of the variable you are referring to variable inside ${}:

# .env
SESSION_DRIVER=mysql
MESSAGE=App based on ${SESSION_DRIVER} database
// PHP
echo env('MESSAGE'); // App based on mysql database

Loading a specific .env file

By default, the package automatically look for the .env file in the project root folder. But you can load the env file from anywhere by using the loadenv function:

// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';

loadenv('/path/to/somewhere/.env');

// Then everything goes as usual
$apiKey = env('API_KEY');

Using the Dotenv instance

You can also get or set a variable using the Dotenv class instance:

The Dotenv instance can be accessed by calling the dotenv() function:

$dotenv = dotenv();

Getting a variable

$hostname = dotenv()->get('DEV_DB_HOST');

// With a default value
$hostname = dotenv()->get('DEV_DB_HOST', 'localhost');

Getting all variables

$hostname = dotenv()->all();

// or use get without any parameter
$hostname = dotenv()->get();

Adding a variable to the current loaded environment

dotenv()->add('GUEST_NAME', 'john');

Writing a variable to the .env file

dotenv()->persist('GUEST_NAME', 'john');

Create your own Dotenv instance

You can create your own Dotenv instance just by using the Dotenv class:

use Prinx\Dotenv;

$dotenv = new Dotenv('path/to/.env');

$dotenv->get('VARIABLE');

Contributing

  • Give a star to the repo ☺️
  • Fork the repo.
  • Correct a bug, add a new feature.
  • Write tests.
  • Create a pull request.

License

MIT

prinx/dotenv 适用场景与选型建议

prinx/dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「env」 「dotenv」 「phpdotenv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 prinx/dotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 prinx/dotenv 我们能提供哪些服务?
定制开发 / 二次开发

基于 prinx/dotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 13
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-27