# 配置

### 程序配置

程序配置分三块，一块是系统配置，一块是程序配置，一块是数据库配置，读取顺序是：程序配置 > 系统配置

```
config/       系统配置路径
app/config/   程序配置路径
#PB#_config   数据库配置表
```

### 配置文件

```
config/config.php     默认配置
config/autoload.php   系统自动加载类的配置
config/exception.php  系统异常类的配置
config/http.php       HTTP请求基本错误码
config/database.php   数据库链接配置
```

用户可通过`config::instance('config')->get`方法获取，简单例子：

```
// config/session.php
return array(
    'session_name' => 'kali_sessionid'
}

// 程序中获取方式 第一个参数为配置文件名，第二个参数为是否使用别名（默认为true）
config::instance('session')->get('session_name', true);
```

### 环境配置

系统对不同环境的配置是可以做区分的，比如：本机开发环境可以使用`dev`，公共测试环境可以使用`pre`，正式环境可以使用`pub`&#x20;

系统配置在`/web/index.php`中

```
// 当前环境 dev pre pub 
defined('SYS_ENV') or define('SYS_ENV', 'dev');
```

当程序调用`config::instance()->get`时，系统会自动查找 `config` 对应的配置文件

```
// 当前环境 dev 会自动查找 config/config_dev.php文件
config::instance('config')->get('test');

// 当前环境 pub 会自动查找 config/database_pub.php文件
config::instance('database')->get('test2');
```

### 别名使用

配置中是支持别名的使用的，在别名两边加上`@`即可，系统默认有个别名 `web`会替换当前路径

```
// config/config.php
return array(
    'path' => '@web@/my-path/'
);

// 返回 'my-path/' 
config::instance('config')->get('path');
```

用户也可以自定义别名，例如：

```
// config->get 之前执行
config::instance('config')->set_alias('time', time());

// config.php
return array(
    'path' => '@web@/my-path/?time=@time@'
);

// 返回 'my-path/?time=1461141347'
config::instance('config')->get('path');

// 返回 '@web@/my-path/?time=@time@'
config::instance('config')->get('path', false);
```

当然如果需要避免别名转义，也可以在`config::instance('config')->get`第二个参数传`false`，就不会执行别名转义了。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.kaliphp.com/config.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
