Slim 框架总是返回 404 错误
这些天我使用 Slim Framework 作为我最简单的工具来开发 php web api.使用这两篇文章:
These days i'm using Slim Framework as my simplest tool to develop the php web api. Using these two articles:
- Coenraets一个>
- CodingThis
我从那里开始遵循一些步骤.下载 Slim 框架,放入正确的目录 &文件.调整启动语句,例如;
I follow some of the steps from there. Downloading the Slim Framework, putting the correct directory & files. Adjusting the initation statements such as;
//1. Require Slim
require('Slim/Slim.php');
//2. Instantiate Slim
$app = new Slim();
//3. Define routes
$app->get('/books', function ($id) {
//Show book with id = $id
});
然后,我相应地修改其余部分.
And then, I modify the rest accordingly.
比如我已经完成的清单:
Such as my checklist that already done:
- LoadModule rewrite_module modules/mod_rewrite.so -> 启用
- 超薄 .htaccess:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule^(.*)$ bootstrap.php [QSA,L]
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ bootstrap.php [QSA,L]
- httpd.conf:(共享链接).
但是,一旦我运行了这个语句;
But, after Once I run this statement;
$app->run();
然后我在我的浏览器上运行它......然后,我在我的本地主机上测试它时遇到了 404 错误.解决这个问题的方法是什么?
And I run it on my browser.... then, I got 404 Error while testing it on my Localhost. What's the solution for fixing that?
仅供参考,这是我目前正在使用的最简单的 PHP 文件.(共享链接)
FYI, here is my simplest PHP file that i'm currently using it. (shared Link)
推荐答案
问题解决了!
我的apache其实是正常的,之前提供的.htaccess文件也正常.
My apache is actually normal, and the .htaccess file provided earlier also normal.
线索是我使用的 URL.以前我使用了无效的 URL,因此它返回了 404 页面错误.当我尝试通过浏览器访问更新的 GET URL 时,我才意识到这一点;
The clue is the URL that I used. Previously I used the invalid URL, thus it returned the 404 page error. I just realized it when I Tried to access the newer GET URL via browser with this one;
http://localhost/dev/index.php/getUsers/user1
现在可以了!
我是在找到这些陈述后才意识到的;
I just realized it once I found these statements;
如果 Slim 没有找到与 HTTP 请求匹配的 URI 的路由URI,Slim 会自动返回 404 Not Found 响应.
If Slim does not find routes with URIs that match the HTTP request URI, Slim will automatically return a 404 Not Found response.
如果 Slim 找到 URI 与 HTTP 请求 URI 匹配但不匹配的路由HTTP 请求方法,Slim 会自动返回一个 405 方法带有 Allow: 标头的不允许响应,其值列出 HTTP所请求资源可接受的方法.
If Slim finds routes with URIs that match the HTTP request URI but not the HTTP request method, Slim will automatically return a 405 Method Not Allowed response with an Allow: header whose value lists HTTP methods that are acceptable for the requested resource.
相关文章