【问题标题】:Restler won't call any API methodsRestler 不会调用任何 API 方法
【发布时间】:2015-03-28 19:09:09
【问题描述】:

Restler 拒绝实例化我的任何 API 类。它只是总是说它在 Route 上失败,但不费心提供任何其他有用的信息。我通过作曲家通过"restler/framework" : "3.0.0-RC6" 安装了 Restler,然后创建了一个如下所示的 index.php:

<?php

require __DIR__.'/../vendor/autoload.php';

use Luracast\Restler\Restler;

$r = new Restler();
$r->addAPIClass('Explorer');
$r->addAPIClass('Play');

$r->handle();

在与 index.php 完全相同的目录中,我创建了一个名为 Play.php 的文件,如下所示:

<?php

public class Play
{
    public function __construct() {
        error_log("I called the constructor!\n", 3, '/tmp/scott');
    }

    public function index() {
        error_log("I called the index\n", 3, '/tmp/scott');
    }

当我调用 http://.../api/play 时,我从未看到 /tmp/scott 文件已创建,而我只是从 Restler 得到一般的失败响应:

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

【问题讨论】:

  • 看起来Play 类没有自动加载。尝试 var_dump(class_exists('Play'));在你的 index.php 中确认
  • 我如何看到该输出?
  • 好的,我用error_log检查了这个值,它肯定说它不存在。
  • 如果我明确地将 Play.php 的要求添加到 index.php 中,它可以工作。不知道这是否可以帮助您解决任何问题。
  • 您必须编辑您的 composer.json 以添加它必须查找源文件的文件夹。在getcomposer.org/doc/01-basic-usage.md#autoloading 阅读有关它您可以在composer.json.jolicode.com 阅读备忘单并在composer.json 中翻转自动加载部分

标签: php restler


【解决方案1】:

正如 Luracast 在 cmets 中指出的,我必须编辑 composer.json 以添加此节:

"autoload": {
    "psr-0": {
        "": "api"
    }
}

然后运行composer dump-autoload

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多