27

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

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 -> enabled
  • Slim .htaccess:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ bootstrap.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?

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

4

8 回答 8

46

Problem is solved!

My apache is actually normal, and the .htaccess file provided earlier also normal.

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

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.

于 2012-03-19T09:32:50.377 回答
28

我在谷歌搜索“slimframework 404”时发现了这篇文章。该帖子使我找到了解决问题的方法。

问题

我使用Composer使用 Slim 框架建立了一个站点,并使用以下示例代码 form slimframework.com 创建了一个 index.php:

<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();

然后我尝试使用http://localhost/hello/bob. 我返回一个 404 Page Not Found 页面。

我能够使用http://localhost/index.php/hello/bob.

解决方案

我通过查看/vendor/slim/.htaccess(包括 Composer Slim 安装)和 Slim 框架文档的URL 重写部分找到了解决方案。

我将该文件的副本添加/vendor/slim/.htaccess到与我的 index.php 文件相同的文件夹中。该文件的内容是:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L] 

现在我可以使用http://localhost/hello/bob.

于 2015-06-04T01:45:33.007 回答
5

您将 index.php 包含在您的文件中是正确的,但这是因为您无法正常使用 mod-rewrite 请检查以下链接:

https://github.com/codeguy/Slim

在入门部分,您可以看到如何配置您的服务器(如果您使用的是 apache / lighttpd / ngynx)

于 2012-06-05T18:09:12.660 回答
1

我认为您的问题出在“资源解析器”上,因为您没有在请求中定义 $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();

请告诉我们是否可以

于 2013-05-30T16:38:36.607 回答
1

对我来说,问题是我忘记.htaccess在我的文档根目录中提供一个文件。

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
于 2017-11-14T11:49:46.940 回答
0

此外mod_rewrite.so,必须加载模块,httpd.conf否则您将收到错误 500。

LoadModule rewrite_module modules/mod_rewrite.so
于 2017-02-15T02:03:57.617 回答
0

如果您在 Ubuntu 16.04 上使用 Slim 并且收到 404 错误。试试上面 Tod Birdsall 的这个测试:

如果这有效

http://localhost/index.php/hello/bob

这不起作用

http://localhost/hello/bob

和您的 .htaccess 文件在与您的 index.php 相同的目录中,配置如下:

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^ index.php [QSA,L]
    </IfModule>

而且您仍然会在 Apache 上收到 404 错误。

尝试如下开启 Apache mod_rewrite 并重启 Apache:

$sudo a2enmod 重写

$sudo 服务 apache2 重启

Slim API 现在应该可以正常工作了,因为提供的 .htaccess 文件只有在 mod_rewrite 启用并且不是全新安装的默认设置时才能工作。

如果需要,这是关闭 mod_rewirte 的方法。

$sudo a2dismod 重写

$sudo 服务 apache2 重启

于 2017-12-28T07:13:30.770 回答
0

对于因为以前不起作用而仍在搜索答案的人,这对我有用:

RewriteBase /<base_of_your_project>/
于 2018-03-27T22:05:54.827 回答