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