Slim框架总是返回404错误。

时间:2021-09-29 07:38:36

These days i'm using Slim Framework as my simplest tool to develop the php web api. Using these two articles:

现在,我使用Slim框架作为开发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框架,放置正确的目录和文件。调整引用语句,如;

//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
  • LoadModule rewrite_module模块/ mod_rewrite。所以- >启用
  • Slim .htaccess:
  • Slim . htaccess:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ bootstrap.php [QSA,L]

RewriteEngine RewriteCond % { REQUEST_FILENAME } ! - f RewriteRule ^(. *)引导美元。php(QSA L)

But, after Once I run this statement;

但是,一旦我运行了这个语句;

$app->run();

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?

我在我的浏览器中运行它....然后,我在我的本地主机上测试了404错误。解决这个问题的办法是什么?

FYI, here is my simplest PHP file that i'm currently using it. (shared Link)

FYI,这是我最简单的PHP文件,我现在正在使用它。(共享链接)

8 个解决方案

#1


42  

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.

如果Slim没有找到与HTTP请求URI匹配的URI的路由,那么Slim将自动返回404 not Found响应。

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找到与HTTP请求URI匹配但与HTTP请求方法不匹配的URI的路由,那么Slim将自动返回一个不允许响应的405方法,其值列出了请求资源可以接受的HTTP方法。

#2


23  

I found this post while googling "slimframework 404". The post led me to a solution to my problem.

我在谷歌“瘦身404”的时候发现了这个帖子。这篇文章使我找到了解决问题的办法。

The Problem

这个问题

I set up a site with the Slim framework using Composer and create an index.php with the following example code form slimframework.com:

我使用Composer创建了一个带有Slim框架的站点,并创建了一个索引。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页未找到的页面。

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 Rewriting section of the Slim framework documentation.

我通过看/卖/卖/瘦/瘦的方法找到了解决办法。htaccess(包括w/ 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:

我增加了一份/供应商/slim/。htaccess文件到与索引相同的文件夹。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访问页面。

#3


5  

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:

你马上就要把索引包括进去。php在你的文件,但这发生因为你没有正确地工作与你的移动-重写请检查以下链接:

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)

在Get Started部分中,您可以看到如何配置服务器(如果您正在使用apache / lighttpd / ngynx)

#4


1  

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]

#5


0  

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

请告诉我们是否可以

#6


0  

Additionally mod_rewrite.so module must be loaded in httpd.conf or you will receive error 500.

此外mod_rewrite。因此,必须在httpd中加载模块。conf或您将收到错误500。

LoadModule rewrite_module modules/mod_rewrite.so

#7


0  

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

这并不工作

http://localhost/hello/bob

http://localhost/hello/bob

and your .htaccess file in the same directory as your index.php and is configured as follows:

你的。htaccess文件和你的索引一样。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服务输入重启

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_rewirte的方法。

$sudo a2dismod rewrite

$ sudo a2dismod重写

$sudo service apache2 restart

$ sudo服务输入重启

#8


0  

For people who still search answers because previous doesn't work, this works for me :

对于那些仍然因为之前的方法不起作用而搜索答案的人来说,这个方法对我很有效:

RewriteBase /<base_of_your_project>/

#1


42  

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.

如果Slim没有找到与HTTP请求URI匹配的URI的路由,那么Slim将自动返回404 not Found响应。

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找到与HTTP请求URI匹配但与HTTP请求方法不匹配的URI的路由,那么Slim将自动返回一个不允许响应的405方法,其值列出了请求资源可以接受的HTTP方法。

#2


23  

I found this post while googling "slimframework 404". The post led me to a solution to my problem.

我在谷歌“瘦身404”的时候发现了这个帖子。这篇文章使我找到了解决问题的办法。

The Problem

这个问题

I set up a site with the Slim framework using Composer and create an index.php with the following example code form slimframework.com:

我使用Composer创建了一个带有Slim框架的站点,并创建了一个索引。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页未找到的页面。

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 Rewriting section of the Slim framework documentation.

我通过看/卖/卖/瘦/瘦的方法找到了解决办法。htaccess(包括w/ 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:

我增加了一份/供应商/slim/。htaccess文件到与索引相同的文件夹。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访问页面。

#3


5  

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:

你马上就要把索引包括进去。php在你的文件,但这发生因为你没有正确地工作与你的移动-重写请检查以下链接:

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)

在Get Started部分中,您可以看到如何配置服务器(如果您正在使用apache / lighttpd / ngynx)

#4


1  

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]

#5


0  

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

请告诉我们是否可以

#6


0  

Additionally mod_rewrite.so module must be loaded in httpd.conf or you will receive error 500.

此外mod_rewrite。因此,必须在httpd中加载模块。conf或您将收到错误500。

LoadModule rewrite_module modules/mod_rewrite.so

#7


0  

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

这并不工作

http://localhost/hello/bob

http://localhost/hello/bob

and your .htaccess file in the same directory as your index.php and is configured as follows:

你的。htaccess文件和你的索引一样。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服务输入重启

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_rewirte的方法。

$sudo a2dismod rewrite

$ sudo a2dismod重写

$sudo service apache2 restart

$ sudo服务输入重启

#8


0  

For people who still search answers because previous doesn't work, this works for me :

对于那些仍然因为之前的方法不起作用而搜索答案的人来说,这个方法对我很有效:

RewriteBase /<base_of_your_project>/