在一个项目中 目录结构是这样的
网站的公共配置,包括一些数据库连接配置,redis连接配置
/web/config/redis_config.php/web/config/mysql_config.php
下面是Easyswoole 项目目录 所有的配置均放在ES项目中的Config目录中 文件名且采用大驼峰命名。如下:
/web/Easyswoole/Config/Redis.php/web/Easyswoole/Config/Mysql.php
我们要从上述的/web/config/redis_config.php文件中读取配置信息,该如何写呢 这里有两个方法 其实是一样的
方法一:
在/web/Easyswoole/Config/Redis.php 文件中 定义常量BASEPATH 然后常量直接引入 代码如下:
<?php
defined('BASEPATH') or define('BASEPATH', dirname(__FILE__) . '/../..');
@include(BASEPATH . '/config/common_config.php'); //引入配置文件//读取config/common_config.php 中的常量 我们可以直接这么写
return ['WEB_IP'=>WEB_IP
];
方法二:
在/web/Easyswoole/Config/Redis.php 文件中 定义一个全局变量 然后 直接引入对应的配置文件 代码如下:
<?php
$database_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . '../../config/common_config.php'; //引入文件
global $redis_config; //定义全局变量@include($database_file);
return ['redis' => [# 默认redis 配置'REDIS' => ['host' => $redis_config['host'],'port' => $redis_config['port'],'auth' => $redis_config['password'],'db' => $redis_config['database'],'serialize' => 0]]
];