我正在使用 PHP 5.4 RC5,并通过终端启动服务器
php -S localhost:8000
目前正在使用 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'];
http://localhost:8000/blog/read/1
的请求按预期工作。
但是当像 http://localhost:8000/blog/read/1.json
, http://localhost:8000/blog/read/这样的点 json 或点 html 1.html
请求来了,php抛出
Not Found
The requested resource /blog/read/1.json was not found on this server.
当我使用内置的 php 服务器运行服务器时,我可以在哪里修复不抛出 html 和 json 文件未找到错误?
或者我想去安装 apache 并启用 mod 重写和其他东西吗?
最佳答案
您正在尝试在未指定的情况下为 PHP 内置网络服务器使用路由器脚本:
php -S localhost:8000
改为添加您的路由器脚本:
php -S localhost:8000 router.php
路由器脚本应该在请求匹配时处理请求,或者它应该返回 FALSE
以防它希望应用标准路由。比较 Built-in web serverDocs .
我不知道 Aura.Router 是否提供对开箱即用的内置 Web 服务器 的支持,或者它是否需要您编写一个适配器为了它。就像您需要为该路由器库配置网络服务器一样,您也需要配置内置网络服务器。这就是上面示例中的 router.php
脚本。
关于PHP 5.4 Built in Server,修复找不到文件的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9007513/