Restler always returns 404: Not found

给你一囗甜甜゛ 提交于 2019-12-03 18:10:03

问题


I have installed Restler on my local server to test and make some APIs for my project.

Api requests are handled via 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:

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:

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)

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.

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?

Edit 2: I just moved the following class into the example 001 folder:

class User {

    function data() {

        return "HELLO!";
    }
}

and added this line inside the index.php of the example:

$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.

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!


回答1:


Here is how you can debug and find whats wrong,

  • Remove the namespace in 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

    $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.

  • 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



来源:https://stackoverflow.com/questions/20488815/restler-always-returns-404-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!