[英]Restler always returns 404: Not found
我已经在我的本地服务器上安装了 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/data
则不是。
在这一点上,我已经失去了所有希望,我真的需要帮助,因为我真的错过了一些东西,但真的无法猜到是什么:(帮助!
以下是您如何调试并找出问题所在,
删除 api.php 中的命名空间。 它不是必需的,会导致问题。
将资源管理器文件夹复制到/myproject/api
文件夹并将资源类添加为 api 类
$restler->addApiClass('Resources'); //this produces the needed resources.json
如果您要添加具有命名空间的 User 类,请确保它在该命名空间下定义并且可用于包含语句或自动加载器。
您可以创建/myproject/api/cache
文件夹并使其可写。 当您在生产模式下使用缓存刷新实例化restler时,如下所示,它将在缓存文件夹中创建routes.php,其中列出了所有路由信息
$restler = new Restler(true,true); //production_mode, refresh
希望你能发现上面的问题
检查以确保您的函数是public
而不是private
。
我实际上只是遇到了同样的问题,只是花了一个小时才弄明白(facepalm)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.