我在本地服务器上安装了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
,它就没有问题。
在这一点上,我已经失去了所有的希望,我真的需要帮助,因为我真的错过了一些东西,但真的猜不到是什么:(help!
以下是如何调试和查找错误,
-
删除api.php中的命名空间。这不是必需的,会导致问题。
-
将资源管理器文件夹复制到
/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
希望您能发现上面的有什么问题
检查以确保您的函数是public
而不是private
。
事实上,我也遇到了同样的问题,只花了一个小时就解决了。