php Slim Framework 总是返回 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9747640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slim Framework always return 404 Error
提问by gumuruh
These days i'm using Slim Frameworkas my simplest tool to develop the php web api. Using these two articles:
这些天我使用Slim Framework作为我最简单的工具来开发 php web api。使用这两篇文章:
I follow some of the steps from there. Downloading the Slim Framework, putting the correct directory & files. Adjusting the initation statements such as;
我遵循了那里的一些步骤。下载 Slim Framework,放入正确的目录和文件。调整启动语句,例如;
//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 -> enabled
- Slim .htaccess:
- 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: (Shared Link).
- httpd.conf:(共享链接)。
But, after Once I run this statement;
但是,一旦我运行了这个语句;
$app->run();
And I run it on my browser.... then, I got 404 Errorwhile testing it on my Localhost. What's the solution for fixing that?
我在我的浏览器上运行它......然后,我在我的本地主机上测试它时遇到了404 错误。解决这个问题的解决方案是什么?
FYI, here is my simplest PHP file that i'm currently using it. (shared Link)
仅供参考,这是我目前正在使用的最简单的 PHP 文件。(共享链接)
回答by gumuruh
Problem is solved!
问题解决了!
My apache is actually normal, and the .htaccess file provided earlier also normal.
我的apache其实是正常的,之前提供的.htaccess文件也正常。
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;
线索是我使用的 URL。以前我使用了无效的 URL,因此它返回了 404 页面错误。当我尝试通过浏览器访问更新的 GET URL 时,我才意识到这一点;
http://localhost/dev/index.php/getUsers/user1
and now that works!
现在可以了!
I just realized it once I found these statements;
一旦我发现这些陈述,我就意识到了;
If Slim does not find routes with URIs that match the HTTP request URI, Slim will automatically return a 404 Not Found response.
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.
如果 Slim 没有找到 URI 与 HTTP 请求 URI 匹配的路由,Slim 将自动返回 404 Not Found 响应。
如果 Slim 发现路由的 URI 与 HTTP 请求 URI 匹配但与 HTTP 请求方法不匹配,则 Slim 将自动返回一个 405 Method Not Allowed 响应,其中包含一个 Allow: 标头,其值列出了所请求资源可接受的 HTTP 方法。
回答by Tod Birdsall
I found this post while googling "slimframework 404". The post led me to a solution to my problem.
我在谷歌搜索“slimframework 404”时发现了这篇文章。该帖子使我找到了解决问题的方法。
The Problem
问题
I set up a site with the Slim framework using Composerand create an index.php with the following example code form slimframework.com:
我使用Composer使用 Slim 框架建立了一个站点,并使用以下示例代码表单 slimframework.com 创建了一个 index.php:
<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
Then I try to access the page using http://localhost/hello/bob
. I get back a 404 Page Not Found page.
然后我尝试使用http://localhost/hello/bob
. 我得到一个 404 Page Not Found 页面。
I was able to get to access the page using http://localhost/index.php/hello/bob
.
我能够使用http://localhost/index.php/hello/bob
.
The Solution
解决方案
I found the solution by looking at /vendor/slim/.htaccess
(included w/ Composer Slim install) and the URL Rewritingsection of the Slim framework documentation.
我通过查看/vendor/slim/.htaccess
(包含 Composer Slim 安装)和Slim 框架文档的URL 重写部分找到了解决方案。
I added a copy of the /vendor/slim/.htaccess
file to the same folder as my index.php file. The contents of the file are:
我将/vendor/slim/.htaccess
文件的副本添加到与 index.php 文件相同的文件夹中。该文件的内容是:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Now I can access the page using http://localhost/hello/bob
.
现在我可以使用http://localhost/hello/bob
.
回答by Darkaico
You're right about to include the index.php on your file, but that happen because you're not working properly with your mod-rewrite Please check the following link:
您将 index.php 包含在您的文件中是正确的,但发生这种情况是因为您的 mod-rewrite 无法正常工作请检查以下链接:
https://github.com/codeguy/Slim
https://github.com/codeguy/Slim
In the Get Started part you could see how to configure your server (in case that you were using apache / lighttpd / ngynx)
在入门部分,您可以看到如何配置您的服务器(如果您使用的是 apache / lighttpd / ngynx)
回答by Magnus W
For me the problem was that I'd forgotten to provide a .htaccess
file in my document root.
对我来说,问题是我忘记.htaccess
在我的文档根目录中提供一个文件。
.htaccess
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
回答by Cosworth66
If you're using Slim on Ubuntu 16.04 and you're getting the 404 error. Try this test from Tod Birdsall above:
如果您在 Ubuntu 16.04 上使用 Slim 并且您收到 404 错误。试试上面 Tod Birdsall 的这个测试:
If this works
如果这有效
http://localhost/index.php/hello/bob
http://localhost/index.php/hello/bob
and this doesnt work
这不起作用
and your .htaccess file in the same directory as your index.php and is configured as follows:
和您的 .htaccess 文件与您的 index.php 位于同一目录中,并配置如下:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
And you still get the 404 error on Apache.
而且您仍然会在 Apache 上收到 404 错误。
Try turning on Apache mod_rewrite as follows and restart Apache:
尝试按如下方式打开 Apache mod_rewrite 并重新启动 Apache:
$sudo a2enmod rewrite
$sudo a2enmod 重写
$sudo service apache2 restart
$sudo 服务 apache2 重启
The Slim API should work correctly now as the provided .htaccess file will only work if mod_rewrite is enabled and that not the default on a clean install.
Slim API 现在应该可以正常工作,因为提供的 .htaccess 文件仅在启用 mod_rewrite 时才有效,而不是全新安装时的默认设置。
Here is how to turn off mod_rewirte if you need to.
如果需要,这是关闭 mod_rewrite 的方法。
$sudo a2dismod rewrite
$sudo a2dismod 重写
$sudo service apache2 restart
$sudo 服务 apache2 重启
回答by Efbi
For people who still search answers because previous doesn't work, this works for me :
对于仍然搜索答案的人,因为以前的不起作用,这对我有用:
RewriteBase /<base_of_your_project>/
回答by Ger Soto
I think your problem is at the "Resource parser" because you are no defining $id parameter at the request so, please, try this:
我认为您的问题出在“资源解析器”上,因为您没有在请求中定义 $id 参数,因此,请尝试以下操作:
//1. Require Slim
require('Slim/Slim.php');
//2. Instantiate Slim
$app = new Slim();
//3. Define routes
$app->get('/books/:id/', function ($id) {
echo json_encode( getBook($id) );
});
// some stuff
$app->run();
Please, tell us if it's ok
请告诉我们是否可以
回答by user2454565
Additionally mod_rewrite.so
module must be loaded in httpd.conf
or you will receive error 500.
此外mod_rewrite.so
必须加载模块,httpd.conf
否则您将收到错误 500。
LoadModule rewrite_module modules/mod_rewrite.so