自定义配置

2021-09-27 10:57:29
admin
1682
最后编辑:admin 于 2023-08-24 13:08:05

用户的自定义配置文件 my.php 中主要包括了系统安装、debug调试模式、URL类型、url分隔符、web路径以及数据库等配置。用户如果想对默认的基本公共配置进行改动,可以将配置项拷贝到自定义配置文件中进行修改即可,config.php 文件会自动加载 my.php 的配置。

<?php
$config->installed    = true;  
$config->debug        = true;  
$config->requestType  = 'GET';    // PATH_INFO 、PATH_INFO2 、 GET.
$config->requestFix   = '-';
$config->webRoot      = '/'; 
$config->db->host     = 'localhost';
$config->db->port     = '3306';
$config->db->name     = 'demo'; 
$config->db->user     = 'root'; 
$config->db->password = '';

当然,除了 my.php 文件,如果你还需要定义其他的配置,可以单独创建相应的配置文件,然后同样在 config.php 中载入即可。下面可以参考禅道的 config.php 载入 /config/目下其他配置的设置:

/* 配置参数过滤。Filter param settings. */
$filterConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'filter.php';
if(file_exists($filterConfig)) include $filterConfig;

/* 引用数据库的配置。 Include the database config file. */
$dbConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'db.php';
if(file_exists($dbConfig)) include $dbConfig;

/* 引用自定义的配置。 Include the custom config file. */
$myConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my.php';
if(file_exists($myConfig)) include $myConfig;

/* 禅道配置文件。zentaopms settings. */
$zentaopmsConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'zentaopms.php';
if(file_exists($zentaopmsConfig)) include $zentaopmsConfig;

/* API路由配置。API route settings. */
$routesConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'routes.php';
if(file_exists($routesConfig)) include $routesConfig;

/* Include extension config files. */
$extConfigFiles = glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ext/*.php');
if($extConfigFiles) foreach($extConfigFiles as $extConfigFile) include $extConfigFile;

打印配置参数

框架提供了 exportConfig() 方法,用来向客户端输出配置参数,客户端可以根据这些参数实现和调整请求的逻辑。

实例:

echo $this->app->exportConfig();

输出效果:

{"version":"3.1","requestType":"PATH_INFO","requestFix":"\/","moduleVar":"m","methodVar":"f","viewVar":"t","sessionVar":"sid","sessionName":"sid","sessionID":"i6a61r503i4flkk9edtk57m996","random":4501,"expiredTime":"1440","serverTime":1652925001}