PHP 5.4内置服务器,修复文件未找到错误

Har*_*K T 3 php auraphp

我使用PHP 5.4 RC5,并通过终端启动服务器

php -S localhost:8000
Run Code Online (Sandbox Code Playgroud)

目前使用Aura.Router,并在根目录下我有index.php文件和代码

<?php
$map = require '/Aura.Router/scripts/instance.php';

$map->add('home', '/');

$map->add(null, '/{:controller}/{:action}/{:id}');

$map->add('read', '/blog/read/{:id}{:format}', [
    'params' => [
        'id' => '(\d+)',
        'format' => '(\.json|\.html)?',
    ],
    'values' => [
        'controller' => 'blog',
        'action' => 'read',
        'format' => '.html',
    ]
]);

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$route = $map->match($path, $_SERVER);
if (! $route) {
    // no route object was returned
    echo "No application route was found for that URI path.";
    exit;
}
echo " Controller : " . $route->values['controller'];
echo " Action : " . $route->values['action'];
echo " Format : " . $route->values['format'];
Run Code Online (Sandbox Code Playgroud)

要求http://localhost:8000/blog/read/1按预期工作.

但当一个点json或点html喜欢http://localhost:8000/blog/read/1.json,http://localhost:8000/blog/read/1.html请求来了,php抛出

Not Found
The requested resource /blog/read/1.json was not found on this server.
Run Code Online (Sandbox Code Playgroud)

当我使用内置的php服务器运行服务器时,我在哪里可以修复不抛出html和json文件找不到错误?

或者我想去安装apache并启用mod重写和东西?

hak*_*kre 11

您正在尝试使用PHP内置Web服务器的路由器脚本而不指定它:

php -S localhost:8000
Run Code Online (Sandbox Code Playgroud)

而是添加您的路由器脚本:

php -S localhost:8000 router.php
Run Code Online (Sandbox Code Playgroud)

如果请求匹配,路由器脚本应该处理请求,或者它应该返回FALSE以防它需要应用标准路由.比较内置Web服务器文档.

我不知道Aura.Router是否支持开箱即用的内置Web服务器,或者它是否需要您为其编写适配器.就像您需要为该路由器库配置Web服务器一样,您也需要配置内置Web服务器.这是router.php上面例子中的脚本.