为什么没做 url rewrite 但是效果却和做了一样?

2015-11-25 12:29:46 +08:00
 waytwoex

在学习 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 规则呢?求解。。。

2319 次点击
所在节点    PHP
1 条回复
haiyang416
2015-11-25 12:37:57 +08:00
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.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/238768

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX