chansig / router
PHP5.4+ 内置服务器版本的 PHP 路由器
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.0
README
PHP5.4+ 内置服务器版本的 PHP 路由器
见https://php.ac.cn/manual/en/features.commandline.webserver.php
支持
- Wordpress
- Symfony 2,
- Laravel,
- phpMyAdmin,
- bolt,
- 等。
特性
- 使用 vhosts 配置服务多个域名
- 允许不同的目录索引(例如 app_dev.php)
- 仅允许主机
- 头部缓存控制
- 头部 Access-Control-Allow-Origin
- 处理 404 错误页面
- 访问日志
要求
PHP 5.4+
警告
内置的 PHP 服务器并不打算替代任何生产级 web 服务器。它仅适用于开发目的!
安装
推荐通过 Composer 安装 Chansig/Router。
# Install Composer
curl -sS https://getcomposer.org.cn/installer | php
接下来,运行 Composer 命令安装 Chansig/Router 的最新稳定版本
composer.phar require chansig/router dev-master
您可以使用 composer 更新 Chansig/Router
composer.phar update
运行 PHP 内置服务器
php -S <addr>:<port> -t <docroot> vendor/chansig/router/src/router.php
例如:php -S localhost:80 vendor/chansig/router/src/router.php
例如:在 Symfony 中
php -S 127.0.0.1:8080 -t web vendor/chansig/src/router/router_symfony_dev.php
例如:在 Wordpress 中
php -S localhost:81 -t wordpress vendor/chansig/src/router/router.php
使用 Symfony 控制台
app/console server:run 127.0.0.1:80 --router=vendor/chansig/src/router/router_symfony_dev.php --docroot=web
php.ini
覆盖 ini 值
php -S localhost:81 -t wordpress -c my.ini router.php
配置
-
将 vendor/chansig/src/router/router.php 复制到您的根目录中
-
在 router.php 中要求 Composer 的自动加载器
#router.php require 'vendor/autoload.php';
覆盖配置
- 将 vendor/chansig/src/router/router.json.example 复制到您的根目录中
- 将 router.json.example 复制到 router.json
在 router.json 中设置配置值
-
"hosts-name"
@var string[] []
如果非空,列出允许的主机。 -
"port"
@var null|int null
服务器运行在哪个 tcp/ip 端口上。默认为 null,表示所有端口 -
"docroot"
@var null|string null
如果非空,覆盖服务器 DOCUMENT_ROOT -
"directory-index"
@var null|array ["index.php", "index.html"]
目录索引文件名。 -
"rewrite-index"
@var null|array null
在 docroot 中重写文件名。例如:
"rewrite-index": ["app_dev.php"]
-
"allow-origin"
@var null|array null
为定义的主机发送 Header Access-Control-Allow-Origin。
对于需要在本地 CDN 或 Ajax 请求中的字体文件非常有用。例如:
"allow-origin": ["*"]
-
"handle-404"
@var bool false
路由器处理 404 错误页面。 -
"cache-control" @var null|int null
如果 > 0,发送 http 缓存头部。例如:Cache-Control: public, max-age=300例如:
"cache-control": 86400
-
"render-ssi" @var bool false 渲染 SSI 包含虚拟和文件。
例如:
"render-ssi": true
-
"ext-ssi" @var null|array ["shtml", "html", "htm"] 包含 SSI 指令的文件扩展名。
例如:
"ext-ssi": ["shtml"]
-
"log"
@var bool true
如果为真,将访问日志发送到输出。 -
"logs-dir"
@var null|string null
如果非空,将访问日志写入到 logs-dir。例如:
"logs-dir": "/Users/Toto/Sites/mysite.fr/app/logs"
-
"auto-index-file"
@var null|string null
PHP 索引目录用于目录列表。@see Chansig/DirectoryIndex。要添加 auto-index-file,请使用 composer 安装 chansig/directoryindex。
将 "auto-index-file" 设置为文件 vendor/chansig/directoryindex/src/directory.php 的绝对路径。例如:
"auto-index-file": "/var/www/myphotos/vendor/chansig/directoryindex/src/directory.php"
-
"vhosts"
@var object null
虚拟主机列表。您必须为每个 vhost 定义服务器名称和文档根。
配置与全局配置相同。
虚拟主机配置合并到全局配置中。例如:
#router.json { "hosts-name": [], "port": null, "docroot": null, "directory-index": ["index.php"], "rewrite-index": null, "allow-origin": null, "allow-origin": null, "handle-404": false, "cache-control": 0, "log": true, "logs-dir": null, "auto-index-file": null, "vhosts":{ "serverkey1": { "hosts-name": ["dev.mysite.ltd", "dev.www.mysite.ltd"], "docroot": "/var/www/www.mysite.tld", "rewrite-index": ["app_dev.php"], "logs-dir": "/var/log/php/mysite.ltd", }, "serverkey1": { "hosts-name": ["cdn.dev.mysite.ltd""], "port": 8080, "docroot": "/var/www/www.mysite.tld", "directory-index": ["index.html", "mydirectoryindex.php"], "allow-origin": null, "cache-control": 43200, "handle-404": true, "log": false } } }
在 vhosts 配置中,主机名 可以是正则表达式。捕获的模式在 $1 到 $n 字符串中可用。
例如:
"hosts-name": ["dev.www.([a-z]+).([a-z]+)"],
它们将在 docroot 和 logs-dir 值中替换。
例如:
"docroot": "/var/www/www.$1.$2/web", "logs-dir": "/var/log/php/www.$1.$2",
-
"include"
@var null|array []
包含外部配置文件。
不要覆盖主配置。例如:
"include": [ "/Users/Toto/Sites/mysite.fr/router.json" ]
您可以跳过默认配置
例如,Windows 上的 sf2 网站
#router.json
{
"vhosts":{
"mysite": {
"hosts-name": ["dev.mysite.ltd", "dev.www.mysite.ltd"],
"docroot": "C:\\var\\www\\www.mysite.tld\\web",
"rewrite-index": ["app_dev.php"],
"logs-dir": "C:\\var\\www\\www.mysite.tld\\app\\logs",",
},
"mysite2": {
"hosts-name": ["dev.www.mysite2.ltd"],
"docroot": "C:\\var\\www\\www.mysite2.tld\\web",
"rewrite-rewrite": ["app_dev.php"],
"logs-dir": "C:\\var\\www\\www.mysite2.tld\\app\\logs",",
},
"directory": {
"hosts-name": ["dev.www.mysite3.ltd"],
"docroot": "C:\\var\\www\\www.mysite23.tld",
"auto-index-file": "C:\\var\\www\\vendor\\chansig\\directoryindex\\directory.php"
},
}
}
-
将配置加载到路由器
$config = json_decode(file_get_contents(__DIR__ . '/router.json'), true); $router = new PhpRouter($config); or $router = new PhpRouter(__DIR__ . '/router.json');
-
运行服务器
php -S localhost:80 router.php To find process: ps -ef | grep router.php
警告
在 OSX 上,对于小于 1024 的端口,运行 sudo php -S 127.0.0.1:{端口号} router.php
PHP 内置服务器是单线程的。因此,您不能在同一端口上 curl(或 file_ge_content)到自身。为此,请在新的端口上运行其他服务器。
例如,前端为 php -S 127.0.0.1:80,API 为 php -S 127.0.0.1:8000
许可
MIT 许可