簡體   English   中英

Restler 總是返回 404: Not found

[英]Restler always returns 404: Not found

我已經在我的本地服務器上安裝了 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則不是。

在這一點上,我已經失去了所有希望,我真的需要幫助,因為我真的錯過了一些東西,但真的無法猜到是什么:(幫助!

以下是您如何調試並找出問題所在,

  • 刪除 api.php 中的命名空間。 它不是必需的,會導致問題。

  • 將資源管理器文件夾復制到/myproject/api文件夾並將資源類添加為 api 類

    $restler->addApiClass('Resources'); //this produces the needed resources.json
  • 如果您要添加具有命名空間的 User 類,請確保它在該命名空間下定義並且可用於包含語句或自動加載器。

  • 您可以創建/myproject/api/cache文件夾並使其可寫。 當您在生產模式下使用緩存刷新實例化restler時,如下所示,它將在緩存文件夾中創建routes.php,其中列出了所有路由信息

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

希望你能發現上面的問題

檢查以確保您的函數是public而不是private

我實際上只是遇到了同樣的問題,只是花了一個小時才弄明白(facepalm)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM