简体   繁体   English

Restler 总是返回 404: Not found

[英]Restler always returns 404: Not found

I have installed Restler on my local server to test and make some APIs for my project.我已经在我的本地服务器上安装了 Restler 来测试并为我的项目制作一些 API。

Api requests are handled via http://localhost/myproject/api/ . Api 请求通过http://localhost/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"
            ]
        }
    }
}

Also this is my .htaccess :这也是我的.htaccess

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

And this is what the api.php looks like:这就是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.我想周围有一些配置错误,但我真的不知道出了什么问题。

(I also tried some solutions on Stackoverflow but they doesn't seem to work for me) (我也在 Stackoverflow 上尝试了一些解决方案,但它们似乎对我不起作用)

Edit:编辑:

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 .我尝试使用以下路径http://localhost/myproject/resources/engine/library/restler/public/examples并且它们正在工作,所以我想这与 Restler 本身的配置有关,因为它没有似乎在http://localhost/myproject/api不起作用。

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.此外,我还尝试在http://localhost/myproject/api/explorer复制 Restler Explorer,但我一直收到错误: 404 : Not Found ../resources.json可能是因为我没有在根文件夹中安装 Restler我的项目。 How am I supposed to install Restler in a different subfolder?我应该如何在不同的子文件夹中安装 Restler?

Edit 2: I just moved the following class into the example 001 folder:编辑 2:我刚刚将以下类移动到示例 001 文件夹中:

class User {

    function data() {

        return "HELLO!";
    }
}

and added this line inside the index.php of the example:并在示例的index.php中添加了这一行:

$r->addAPIClass('User');

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.如果我尝试在.../_001_helloworld/index.php/say/hello运行该示例,它可以正常工作,但如果我尝试_001_helloworld/index.php/user/data则不是。

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,以下是您如何调试并找出问题所在,

  • Remove the namespace in api.php.删除 api.php 中的命名空间。 It is not required and will cause issues.它不是必需的,会导致问题。

  • Copy explorer folder to /myproject/api folder and add Resources class as an api class将资源管理器文件夹复制到/myproject/api文件夹并将资源类添加为 api 类

    $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.如果您要添加具有命名空间的 User 类,请确保它在该命名空间下定义并且可用于包含语句或自动加载器。

  • You can create /myproject/api/cache folder and make it writable.您可以创建/myproject/api/cache文件夹并使其可写。 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时,如下所示,它将在缓存文件夹中创建routes.php,其中列出了所有路由信息

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

Hope you find whats wrong with the above希望你能发现上面的问题

Check to make sure your function is public not private .检查以确保您的函数是public而不是private

I actually just had the same problem and just spent an hour figuring it out (facepalm).我实际上只是遇到了同样的问题,只是花了一个小时才弄明白(facepalm)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM