Restler始终返回404:未找到

sia*_*one 5 php .htaccess restler

我在本地服务器上安装了Restler来测试并为我的项目制作一些API.

Api请求通过http://localhost/myproject/api/.处理.

问题是每次我尝试发出请求时都会收到以下错误:

{
    "error": {
        "code": 404,
        "message": "Not Found"
    },
    "debug": {
        "source": "Routes.php:383 at route stage",
        "stages": {
            "success": [
                "get"
            ],
            "failure": [
                "route",
                "negotiate",
                "message"
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这也是我的.htaccess:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api(.*)$ /myproject/api/api.php [QSA,L]
Run Code Online (Sandbox Code Playgroud)

这就是api.php看起来像:

namespace myproject;

use Luracast\Restler\Restler;

require_once($_SERVER["DOCUMENT_ROOT"]."/myproject/resources/engine/settings.php");
require_once(Settings\Path\Absolute::$library."/restler/vendor/restler.php");

$restler = new Restler();
$restler->addAPIClass("myproject\\User");
$restler->handle();
Run Code Online (Sandbox Code Playgroud)

我猜周围有一些误配,但我真的无法弄清楚出了什么问题.

(我也在Stackoverflow上尝试了一些解决方案,但它们似乎对我不起作用)

编辑:

我尝试使用以下路径到达示例 http://localhost/myproject/resources/engine/library/restler/public/examples并且它们正在工作,所以我想它与Restler本身的配置有关,因为它似乎无法工作http://localhost/myproject/api.

此外,我还尝试复制Restler Explorer http://localhost/myproject/api/explorer,我一直收到错误:404 : Not Found ../resources.json可能是因为我没有在我的项目的根文件夹中安装Restler.我怎么能在不同的子文件夹中安装Restler?

编辑2: 我刚刚将以下类移到示例001文件夹中:

class User {

    function data() {

        return "HELLO!";
    }
}
Run Code Online (Sandbox Code Playgroud)

index.php在示例中添加了这一行:

$r->addAPIClass('User');
Run Code Online (Sandbox Code Playgroud)

如果我尝试运行.../_001_helloworld/index.php/say/hello它的例子它没有问题,但如果我尝试_001_helloworld/index.php/user/data它不是.

在这一点上,我已经失去了所有的希望,我真的需要帮助'因为我真的错过了什么,但实在无法猜到:( 帮助!

Aru*_*ran 3

以下是如何调试并找出错误的方法,

  • 删除 api.php 中的命名空间。这不是必需的,并且会导致问题。

  • 将资源管理器文件夹复制到/myproject/api文件夹并将 Resources 类添加为 api 类

    $restler->addApiClass('Resources'); //this produces the needed resources.json
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果您要添加带有命名空间的 User 类,请确保它在该命名空间下定义并可通过 include 语句或自动加载器使用。

  • 您可以创建/myproject/api/cache文件夹并使其可写。当您在生产模式下使用缓存刷新实例化 Restler 时,如下所示,它将在缓存文件夹中创建routes.php,其中列出了所有路由信息

    $restler = new Restler(true,true); //production_mode, refresh
    
    Run Code Online (Sandbox Code Playgroud)

希望您发现以上内容有什么问题