Restler总是返回404:未找到 [英] Restler always returns 404: Not found
问题描述
我有我的本地服务器来测试,使一些API为我的项目上安装Restler。
I have installed Restler on my local server to test and make some APIs for my project.
API请求是通过处理HTTP://本地主机/ myproject的/ API /
的问题是,每次我试图提出一个请求,我得到了以下错误:
The problem is that everytime i try to make a request i get the following error:
{
"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();
我想有周围的一些错误配置,但我真的无法弄清楚什么是错的。
I guess there's some misconfiguration around, but i really can't figure out what's wrong.
(我也试过在#1的一些解决方案,但他们似乎并没有为我工作)
(I also tried some solutions on Stackoverflow but they doesn't seem to work for me)
编辑:
我试着用下面的路径到达例如的http://本地主机/ myproject的/资源/发动机/库/ restler /公/例子
和他们的工作,所以我想它做Restler本身的配置,因为它似乎并没有在工作的http://本地主机/ myproject的/ API
I tried reaching the example using the following path http://localhost/myproject/resources/engine/library/restler/public/examples
and they're working, so i guess it has to do with the configuration of Restler itself because it doesn't seem to work in http://localhost/myproject/api
.
此外,我还试图复制Restler Explorer在的http://本地主机/ myproject的/ API /资源管理器
,我不断收到错误: 404 :未找到../ resources.json
可能是因为我没有在我的项目的根文件夹中安装Restler。我怎么在不同的子文件夹中安装Restler?
Furthermore i also tried copying the Restler Explorer in http://localhost/myproject/api/explorer
and i keep receiving the error: 404 : Not Found ../resources.json
probably because i didn't install Restler in the root folder of my project. How am I supposed to install Restler in a different subfolder?
编辑2: 我刚搬到下面的类到示例001文件夹:
Edit 2: I just moved the following class into the example 001 folder:
class User {
function data() {
return "HELLO!";
}
}
和加入这一行的的index.php
的例子里面:
and added this line inside the index.php
of the example:
$r->addAPIClass('User');
如果我尝试运行的例子在 ... / _ 001_helloworld / index.php文件/说/你好
它的工作原理没有问题,但如果我尝试 _001_helloworld / index.php文件/用户/数据
事实并非如此。
If i try to run the example at .../_001_helloworld/index.php/say/hello
it works without problems but if i try _001_helloworld/index.php/user/data
it isn't.
在这一点上我已经失去了所有的希望,我真的需要帮助'原因我真的失去了一些东西,但实在无法猜测什么:( 帮助!
At this point i have lost every hope and i really need help 'cause really i'm missing something but really can't guess what :( HELP!
推荐答案
下面是你如何调试,发现什么是错,
Here is how you can debug and find whats wrong,
-
删除在api.php的命名空间。它不是必需的,并会引起问题。
Remove the namespace in api.php. It is not required and will cause issues.
复制Explorer文件夹,以 / myproject的/ API
文件夹并添加资源类作为一个API的类
Copy explorer folder to /myproject/api
folder and add Resources class as an api class
$restler->addApiClass('Resources'); //this produces the needed resources.json
如果您要添加的用户类,一个命名空间,确保它定义在该命名空间,并与您的包括陈述或自动加载磁带机提供。
If you are adding User class with a namespace, make sure it defined under that namespace and made available with your include statements or autoloader.
您可以创建 / MyProject的/ API /缓存
文件夹,并使其可写。当你实例化的生产模式,缓存刷新restler如下图所示,它会创建routes.php文件的缓存文件夹中列出的所有路由信息。
You can create /myproject/api/cache
folder and make it writable. When you instantiate restler in production mode with cache refresh as shown below, it will create routes.php in cache folder which lists all the routes information
$restler = new Restler(true,true); //production_mode, refresh
希望你能找到什么错与上述
Hope you find whats wrong with the above
这篇关于Restler总是返回404:未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!