V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
waytwoex
V2EX  ›  PHP

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

  •  
  •   waytwoex · 2015-11-25 12:29:46 +08:00 · 2372 次点击
    这是一个创建于 3288 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在学习 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 条回复    2015-11-25 12:37:57 +08:00
    haiyang416
        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.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3758 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:42 · PVG 18:42 · LAX 02:42 · JFK 05:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.