在学习 slim framework ,用 mac 下直接 php -S localhost:8085
启动的一个示例代码:
require '../vendor/autoload.php';
define("WWW_PATH", dirname(realpath(__FILE__)));
define("ROOT_PATH", dirname(WWW_PATH));
$app = new \Slim\App();
$app->group('/utils', function () use ($app) {
$app->get('/date', function ($request, $response) {
return $response->getBody()->write(date('Y-m-d H:i:s'));
});
$app->get('/time', function ($request, $response) {
return $response->getBody()->write(time());
});
})->add(function ($request, $response, $next) {
$response->getBody()->write('It is now ');
$response = $next($request, $response);
$response->getBody()->write('. Enjoy!');
return $response;
});
$app->run();
这时候直接访问 http://localhost:8085/utils/time 就可以看到效果。
比较奇怪,这个 url 明显是 rewrite 过的,然而我并没有做 rewrite 。按理说不应该请求到./utils/time 目录下去么?
web 目录下也没有.htaccess 之类的文件,为啥自动做了 rewrite ?
难道 php 自带的 web server 会自动给我做 rewrite ?但它怎么会知道我的 rewrite 规则呢?求解。。。
1
haiyang416 2015-11-25 12:37:57 +08:00 2
http://php.net/manual/en/features.commandline.webserver.php
URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, the lookup for index.php and index.html will be continued in the parent directory and so on until one is found or the document root has been reached. If an index.php or index.html is found, it is returned and $_SERVER['PATH_INFO'] is set to the trailing part of the URI. Otherwise a 404 response code is returned. |