0

我试图将 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)
....

怎么了?

4

1 回答 1

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
}
于 2018-02-14T14:29:22.180 回答