【问题标题】:PHP Slim API 404 not found未找到 PHP Slim API 404
【发布时间】:2015-07-08 19:44:48
【问题描述】:

我正在尝试使用 php 和 php slim 创建一个 API。

我的文件夹结构:

API:

  • app -> app.js
  • 库 -> 苗条
  • v1 -> .htaccess 和 index.php

htaccess

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

index.php

require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

$app->get('/orderoverview/:customerID', function ($customerID) {
        echo "Hello " . $customerID;

});
$app->run();

这应该很容易,但每次我去http://localhost/api/v1/orderoverview/2 我都会得到一个 404 Page Not Found。

但是当我转到http://localhost/api/v1/index.php/orderoverview/2 时,我确实得到了我想要的结果!

有什么意见或解决办法吗?

【问题讨论】:

    标签: php .htaccess slim


    【解决方案1】:

    您需要将 RewriteBase 添加到您的 htaccess。我注意到你没有分组,所以我假设你把它放在 /api/v1 的子目录中

    RewriteBase /localsites/serf/weap/api/v1
    

    【讨论】:

    • 当我这样做时,我得到一个:服务器 500 错误!服务器遇到内部错误,无法完成您的请求。服务器过载或 CGI 脚本出错。
    • 我相信它是斜线。这不是必需的。如果它仍然是 500,那么它是代码中的东西而不是 htaccess。
    • 如果这仍然不起作用,也可能是我没有您环境的所有信息。您需要沿着从 Apache 根目录到托管应用程序的子目录的路径遵循重写基础
    • 我仍然收到服务器错误。我的完整路径是:localhost/localsites/serf/weap/api/v1/index.php/orderoverview/5
    • 更新了答案以匹配完整路径我也完全意识到我的答案中有一个冒号。花很多时间写 JSON :)
    【解决方案2】:

    您必须在您的根目录下创建.htaccess 文件并放入以下代码。

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /api/v1/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ %1 [R=301,L]
    

    【讨论】:

    • 我被重定向到我的 xampp 页面,当我在 url 中使用 index.php 时,我得到一个 404
    【解决方案3】:

    我为你创建了这个,只需将你的 .httpaccess 文件更改为 -

    DirectoryIndex /v1/index.php
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
    

    这里 v1 是子文件夹, index.php 是你的函数所在的位置。 希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2017-09-13
      • 2020-01-22
      相关资源
      最近更新 更多