【问题标题】:Why am I getting "Error: Call to a member function end() on null" using Aerys?为什么我使用 Aerys 收到“错误:在 null 上调用成员函数 end()”?
【发布时间】:2018-02-13 18:54:50
【问题描述】:

我试图将 aerys 放在我的 cms 上,但出现错误。 正如我所看到的,后端返回的不是空值——堆栈跟踪中的#1,但它没有达到 $resp->end()。 我被困在试图让这个值到达 end() 的过程中。

代码示例

$router = router()
        ->route("GET", "/exchangews", $websocket)
        ->route('*', '/{resource}/?', function (Aerys\Request $req, Aerys\Response $res, array $route) {

            // copy params and make scalar the single values
            $params = [];
            foreach(($param = yield $req->getAllParams()) as $key => $val){ 
                if(count($val)==1)$params[$key] = array_pop($val);
            }
            $headers = $req->getAllHeaders();

            $bodyProm = Amp\call( function() use($params, $headers, $route){
                try{
                    $lead = new RequestHandler($headers, array_merge($params, $route));
                    $body = $lead->proccess();
                    return $body;
                }catch(\Exception $e){
                    //
                }
            });
            $body = yield $bodyProm;
            // run my application
            $resp->end($body);

            # matched by e.g. /user/rdlowrey/42
            # but not by /user/bwoebi/foo (note the regex requiring digits)
            # $res->end("The user with name {$route['name']} and id {$route['id']} has been requested!");
        });

堆栈跟踪:

    Error: Call to a member function end() on null in /Library/WebServer/Documents/ingine/index_aerys.php:47
    Stack trace:
    #0 [internal function]: {closure}(Object(Aerys\StandardRequest), Object(Aerys\StandardResponse), Array)
    #1 /Library/WebServer/Documents/ingine/vendor/amphp/amp/lib/Coroutine.php(74): Generator->send('<!DOCTYPE html ...')
    #2 /Library/WebServer/Documents/ingine/vendor/amphp/amp/lib/Success.php(33): Amp\Coroutine->Amp\{closure}(NULL, Array)
....

怎么了?

【问题讨论】:

  • 您在任何地方定义$resp。那应该是$res-&gt;end()吗?
  • 另外,您是否在文件中包含 Aerys 库?
  • 使用什么库并不重要。基础不变。您需要包含库并定义变量。现在您可能正在其他地方执行此操作,但我在您提供的代码中没有看到它,这就是我要求确认的原因。
  • 随心所欲地批评我,我几乎肯定你应该在路由功能中使用Aerys\Response $resp 而不是$res
  • 太好了!对不起,我批评了你这么多,我花了这么多时间,很累。

标签: php amphp


【解决方案1】:

这似乎是一个简单的错字。您将Aerys\Response $res 定义为参数,然后使用$resp 并尝试在其上调用未定义的函数。您应该在开发期间启用错误报告。 PHP 应该为那里的未定义变量发出通知。

另外,您的Amp\call 是不必要的。你可以简单地写成:

try {
    $lead = new RequestHandler($headers, array_merge($params, $route));
    $body = yield $lead->proccess();
} catch(\Exception $e){
    // handle error
}

【讨论】:

  • 哦,谢谢你的提示,我花了很多时间试图理解正确的语法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2020-01-19
  • 2017-09-14
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多