Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

php - Restler always returns 404: Not found

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 LuracastRestlerRestler;

require_once($_SERVER["DOCUMENT_ROOT"]."/myproject/resources/engine/settings.php");
require_once(SettingsPathAbsolute::$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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...