php - ReSTLer 总是返回 404 : Not found

标签 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"
            ]
        }
    }
}

这也是我的.htaccess:

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

这就是 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();

我猜周围有一些配置错误,但我真的不知道哪里出了问题。

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

编辑:

我尝试使用以下路径访问示例 http://localhost/myproject/resources/engine/library/reSTLer/public/examples 并且他们正在工作,所以我想它必须处理 ReSTLer 本身的配置,因为它似乎在 http://localhost/myproject/api 中不起作用。

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

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

class User {

    function data() {

        return "HELLO!";
    }
}

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

$r->addAPIClass('User');

如果我尝试在 .../_001_helloworld/index.php/say/hello 运行示例,它可以正常工作,但如果我尝试 _001_helloworld/index.php/user/数据它不是。

在这一点上,我已经失去了所有希望,我真的需要帮助,因为我真的错过了一些东西,但真的猜不出是什么:( HELP!

最佳答案

这是调试和发现问题的方法,

  • 删除 api.php 中的 namespace 。这不是必需的,会导致问题。

  • 将explorer文件夹复制到/myproject/api文件夹并添加Resources类作为api类

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

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

    $restler = new Restler(true,true); //production_mode, refresh
    

希望你能发现上面的问题

关于php - ReSTLer 总是返回 404 : Not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488815/

相关文章:

javascript - 来自多个文件的 Websocket 客户端调用

php - 如何在 post 方法登录中正确使用 MySQLi 函数,这就是我的做法。但有一个错误

php - PHP 函数中 return 语句之后的代码

apache - .htaccess 使用 laravel 重定向到 ssl

regex - 301 将旧网址重定向到移动网站中的新网址

php - 所有 Url 的 ReSTLer 都出现 404 错误

php - 如何正确格式化 PDO 结果? - 以字符串形式返回的数字结果?

php - $(Document).ready 不会触发代码,但会在控制台中运行

php - 当我使用 .htaccess 重定向所有内容时,Bootstrap css 不工作

node.js - 使用 Node 和 reSTLer 进行多部分表单数据 POST