webman:用env配置环境变量(v1.5.7)
一,官方文档地址:
https://www.workerman.net/doc/webman/components/env.html
二,安装phpdotenv插件:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer require vlucas/phpdotenv
安装完成后查看版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show vlucas/phpdotenv
name : vlucas/phpdotenv
descrip. : Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
keywords : dotenv, env, environment
versions : * v5.4.1
...
三,使用配置
1,创建.env
liuhongdi@lhdpc:/data/webman/imageadmin$ vi .env
内容:
liuhongdi@lhdpc:/data/webman/imageadmin$ more .env
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_NAME = yourbasename
DB_USER = root
DB_PASSWORD = yourpassword
2,调用:
config/database.php
说明;用getenv函数来获取变量的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?php /** * This file is part of webman. * * Licensed under The MIT License * For full copyright and license information, please see the MIT-LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @author walkor<walkor@workerman.net> * @copyright walkor<walkor@workerman.net> * @link http://www.workerman.net/ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ return [ // 默认数据库 'default' => 'mysql' , // 各种数据库配置 'connections' => [ 'mysql' => [ 'driver' => 'mysql' , 'host' => getenv ( 'DB_HOST' ), 'port' => getenv ( 'DB_PORT' ), 'database' => getenv ( 'DB_NAME' ), 'username' => getenv ( 'DB_USER' ), 'password' => getenv ( 'DB_PASSWORD' ), 'unix_socket' => '' , 'charset' => 'utf8mb4' , 'collation' => 'utf8mb4_0900_ai_ci' , 'prefix' => '' , 'strict' => true, 'engine' => null, 'options' => [ \PDO::ATTR_TIMEOUT => 3 ] ], ], ]; |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/22/webman-yong-env-pei-zhi-huan-jing-bian-liang/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,查看webman版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...