配置文件
位置
webman的配置文件在config/
目錄下,項目中可以通過config()
函數(shù)來獲取對應的配置。
獲取配置
獲取所有配置
config();
獲取config/app.php
里的所有配置
config('app');
獲取config/app.php
里的debug
配置
config('app.debug');
如果配置是數(shù)組,可以通過.
來獲取數(shù)組內(nèi)部元素的值,例如
config('file.key1.key2');
默認值
config($key, $default);
config通過第二個參數(shù)傳遞默認值,如果配置不存在則返回默認值。
配置不存在且沒有設置默認值則返回null。
自定義配置
開發(fā)者可以在config/
目錄下添加自己的配置文件,例如
config/payment.php
<?php
return [
'key' => '...',
'secret' => '...'
];
獲取配置時使用
config('payment');
config('payment.key');
config('payment.key');
更改配置
webman不支持動態(tài)修改配置,所有配置必須手動修改對應的配置文件,并reload或restart重啟
注意
服務器配置config/server.php
以及進程配置config/process.php
不支持reload,需要restart重啟才能生效
特別提醒
如果你是要在config下的子目錄創(chuàng)建配置文件并讀取,比如:config/order/status.php
,那么config/order
目錄下需要有一個app.php
文件,內(nèi)容如下
<?php
return [
'enable' => true,
];
enable
為true
代表讓框架讀取這個目錄的配置。
最終配置文件目錄樹類似下面這樣
├── config
│?? ├── order
│?? │?? ├── app.php
│?? │?? └── status.php
這樣你就可以通過config.order.status
讀取status.php
中返回的數(shù)組或者特定的key數(shù)據(jù)了。
配置文件講解
server.php
return [
'listen' => 'http://0.0.0.0:8787', // 監(jiān)聽端口(從1.6.0版本開始移除, 改在config/process.php中配置)
'transport' => 'tcp', // 傳輸層協(xié)議(從1.6.0版本開始移除, 改在config/process.php中配置)
'context' => [], // ssl等配置(從1.6.0版本開始移除, 改在config/process.php中配置)
'name' => 'webman', // 進程名(從1.6.0版本開始移除, 改在config/process.php中配置)
'count' => cpu_count() * 4, // 進程數(shù)量(從1.6.0版本開始移除, 改在config/process.php中配置)
'user' => '', // 用戶(從1.6.0版本開始移除, 改在config/process.php中配置)
'group' => '', // 用戶組(從1.6.0版本開始移除, 改在config/process.php中配置)
'reusePort' => false, // 是否開啟端口復用(從1.6.0版本開始移除, 改在config/process.php中配置)
'event_loop' => '', // 事件循環(huán)類,默認自動選擇
'stop_timeout' => 2, // 收到stop/restart/reload信號時,等待處理完成的最大時間,超過這個時間進程未退出則強制退出
'pid_file' => runtime_path() . '/webman.pid', // pid文件存儲位置
'status_file' => runtime_path() . '/webman.status', // status文件存儲位置
'stdout_file' => runtime_path() . '/logs/stdout.log', // 標準輸出文件位置,webman啟動后所有輸出都會寫入這個文件
'log_file' => runtime_path() . '/logs/workerman.log', // workerman日志文件位置
'max_package_size' => 10 * 1024 * 1024 // 最大數(shù)據(jù)包大小,10M。上傳文件大小受到此限制
];
app.php
return [
'debug' => true, // 是否開啟debug模式,開啟后頁面報錯會輸出更多調(diào)試信息
'error_reporting' => E_ALL, // 錯誤報告級別
'default_timezone' => 'Asia/Shanghai', // 默認時區(qū)
'public_path' => base_path() . DIRECTORY_SEPARATOR . 'public', // public目錄位置
'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime', // runtime目錄位置
'controller_suffix' => 'Controller', // 控制器后綴
'controller_reuse' => false, // 控制器是否復用
];
process.php
use support\Log;
use support\Request;
use app\process\Http;
global $argv;
return [
// webman進程配置
'webman' => [
'handler' => Http::class, // 進程處理類
'listen' => 'http://0.0.0.0:8787', // 監(jiān)聽地址
'count' => cpu_count() * 4, // 進程數(shù)量,默認cpu的4倍
'user' => '', // 進程運行的用戶,應該使用低級別用戶
'group' => '', // 進程運行的用戶組,應該使用低級別用戶組
'reusePort' => false, // 是否開啟reusePort,開啟后連接會均勻分布到不同的worker進程
'eventLoop' => '', // 事件循環(huán)類,為空時自動使用server.event_loop配置
'context' => [], // 監(jiān)聽上下文配置,例如ssl
'constructor' => [ // 進程處理類構(gòu)造函數(shù)參數(shù),本例中是Http類的構(gòu)造函數(shù)參數(shù)
'requestClass' => Request::class, // 可以自定義請求類
'logger' => Log::channel('default'), // 日志實例
'appPath' => app_path(), // app目錄位置
'publicPath' => public_path() // public目錄位置
]
],
// 監(jiān)控進程,用于檢測文件更新自動加載和內(nèi)存泄漏
'monitor' => [
'handler' => app\process\Monitor::class, // 處理類
'reloadable' => false, // 當前進程不執(zhí)行reload
'constructor' => [ // 進程處理類構(gòu)造函數(shù)參數(shù)
// 監(jiān)聽的目錄,不要過多,會導致檢測變慢
'monitorDir' => array_merge([
app_path(),
config_path(),
base_path() . '/process',
base_path() . '/support',
base_path() . '/resource',
base_path() . '/.env',
], glob(base_path() . '/plugin/*/app'), glob(base_path() . '/plugin/*/config'), glob(base_path() . '/plugin/*/api')),
// 監(jiān)聽這些后綴文件的更新
'monitorExtensions' => [
'php', 'html', 'htm', 'env'
],
// 其它選項
'options' => [
// 是否開啟文件監(jiān)控,僅在linux下有效,默認守護進程模式不開啟文件監(jiān)控
'enable_file_monitor' => !in_array('-d', $argv) && DIRECTORY_SEPARATOR === '/',
// 是否開啟內(nèi)存監(jiān)控,僅支持在linux下開啟
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
]
]
]
];
container.php
// 返回一個psr-11依賴注入容器實例
return new Webman\Container;
dependence.php
// 用于配置依賴注入容器中的服務和依賴關(guān)系
return [];
route.php
use support\Route;
// 定義/test路徑的路由
Route::any('/test', function (Request $request) {
return response('test');
});
view.php
use support\view\Raw;
use support\view\Twig;
use support\view\Blade;
use support\view\ThinkPHP;
return [
'handler' => Raw::class // 默認視圖處理類
];
autoload.php
// 配置框架自動加載的文件
return [
'files' => [
base_path() . '/app/functions.php',
base_path() . '/support/Request.php',
base_path() . '/support/Response.php',
]
];
cache.php
// 緩存配置
return [
'default' => 'file', // 默認文件
'stores' => [
'file' => [
'driver' => 'file',
'path' => runtime_path('cache') // 緩存文件存儲位置
],
'redis' => [
'driver' => 'redis',
'connection' => 'default' // redis連接名,對應redis.php里的配置
],
'array' => [
'driver' => 'array' // 內(nèi)存緩存,重啟后失效
]
]
];
redis.php
return [
'default' => [
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'database' => 0,
],
];
database.php
return [
// 默認數(shù)據(jù)庫
'default' => 'mysql',
// 各種數(shù)據(jù)庫配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'webman',
'username' => 'webman',
'password' => '',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'sqlite' => [
'driver' => 'sqlite',
'database' => '',
'prefix' => '',
],
'pgsql' => [
'driver' => 'pgsql',
'host' => '127.0.0.1',
'port' => 5432,
'database' => 'webman',
'username' => 'webman',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => 'localhost',
'port' => 1433,
'database' => 'webman',
'username' => 'webman',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
],
],
];
exception.php
return [
// 設置異常處理類
'' => support\exception\Handler::class,
];
log.php
return [
'default' => [
'handlers' => [
[
'class' => Monolog\Handler\RotatingFileHandler::class, // 處理器
'constructor' => [
runtime_path() . '/logs/webman.log', // 日志名
7, //$maxFiles // 保留7天內(nèi)的日志
Monolog\Logger::DEBUG, // 日志級別
],
'formatter' => [
'class' => Monolog\Formatter\LineFormatter::class, // 格式化器
'constructor' => [null, 'Y-m-d H:i:s', true], // 格式化參數(shù)
],
]
],
],
];
session.php
return [
// 類型
'type' => 'file', // or redis or redis_cluster
// 處理器
'handler' => FileSessionHandler::class,
// 配置
'config' => [
'file' => [
'save_path' => runtime_path() . '/sessions', // 存儲目錄
],
'redis' => [
'host' => '127.0.0.1',
'port' => 6379,
'auth' => '',
'timeout' => 2,
'database' => '',
'prefix' => 'redis_session_',
],
'redis_cluster' => [
'host' => ['127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7001'],
'timeout' => 2,
'auth' => '',
'prefix' => 'redis_session_',
]
],
'session_name' => 'PHPSID', // session名
'auto_update_timestamp' => false, // 是否自動更新時間戳,避免session過期
'lifetime' => 7*24*60*60, // 生命周期
'cookie_lifetime' => 365*24*60*60, // cookie生命周期
'cookie_path' => '/', // cookie路徑
'domain' => '', // cookie域
'http_only' => true, // 僅http訪問
'secure' => false, // 僅https訪問
'same_site' => '', // SameSite屬性
'gc_probability' => [1, 1000], // session回收概率
];
middleware.php
// 設置中間件
return [];
static.php
return [
'enable' => true, // 是否開啟webman的靜態(tài)文件訪問
'middleware' => [ // 靜態(tài)文件中間件,可用于設置緩存策略、跨域等
//app\middleware\StaticFile::class,
],
];
translation.php
return [
// 默認語言
'locale' => 'zh_CN',
// 回退語言
'fallback_locale' => ['zh_CN', 'en'],
// 語言文件存儲位置
'path' => base_path() . '/resource/translations',
];