【问题标题】:Custom base path for Restler?Restler 的自定义基本路径?
【发布时间】:2015-07-18 10:50:44
【问题描述】:

当 Restler 未部署在域/子域的根目录时,推荐/假定的使用自定义基本路径的方法是什么?

我的网站有自己的路由,我在这里使用Restler 3.0:

http://www.example.com/api/data/movies

当它路由到 /api/data/movies 时,我让 Restler 负责:

class Say {
    function hello($to='world') {
        return "Hello $to!";
    }
    function hi($to) {
        return  "Hi $to!";
    }
}

use Luracast\Restler\Restler;
$r = new Restler();
$r->addAPIClass('Say', '/api/data/movies/say');
$r->handle();

但是,不知何故,它没有通过子路径获取传递给它的参数。见下文。

example.com/api/data/movies/say/hi

{
    "error": {
        "code": 400,
        "message": "Bad Request: `to` is required."
    },
    "debug": {
        "source": "Validator.php:366 at validate stage",
        "stages": {
            "success": [
                "get",
                "route",
                "negotiate"
            ],
            "failure": [
                "validate",
                "message"
            ]
        }
    }
}

example.com/api/data/movies/say/hi/Jim

{
    "error": {
        "code": 404,
        "message": "Not Found"
    },
    "debug": {
        "source": "Routes.php:431 at route stage",
        "stages": {
            "success": [
                "get"
            ],
            "failure": [
                "route",
                "negotiate",
                "message"
            ]
        }
    }
}

我尝试使用 Google 搜索,但没有发现与我的问题相关的内容。我怎样才能让它工作?在子目录 URL 而不是域/子域根目录下使用 Restler 的推荐/假定方法是什么?

【问题讨论】:

    标签: php rest restler


    【解决方案1】:

    根据 Restler 文档

    你必须在你的URL请求中传递to参数,所以我认为你的URL应该是example.com/api/data/movies/say/hi?to=test

    但是如果你想添加自定义路由,那么请检查Restler Routing Works

    此 API Server 公开以下 URI

    GET say/hi/{to} ⇠ Say::hi()

    我对 Restler 不熟悉,但希望对您有所帮助。

    【讨论】:

    • 为什么会说“所需的原始类型将映射到 url 路径”here
    【解决方案2】:

    使用自动路由所需的原始类型用于映射到 url 路径,此行为在最近的版本中发生了变化。很抱歉造成混乱,我们现在更新了文档!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多