Slim framework我遇到此问题。我有类模板有渲染方法,我希望Slim在路由处理程序返回时呈现此类的对象
$app->get('/test', function() {
return new Template('main', function() use ($error) {
return array(
'content' => "Hello"
);
});
});
它工作我创建了子类(在System.php中)
class System extends Slim {
function __constructor() {
Slim::__construct();
}
private function auto_render_fun($callable) {
$app = $this;
return function() use ($callable, $app) {
$args = func_get_args();
$ret = call_user_func_array($callable, $args);
if ($ret instanceof Template) {
//render Template - Slim ignore return value
$app->response()->body($ret->render());
}
};
}
protected function mapRoute($args) {
$last = count($args)-1;
$args[$last] = $this->auto_render_fun($args[$last]);
return Slim::mapRoute($args);
}
}
我想用notFound
做同样的事情$app->notFound(function () use ($app) {
$response = $app->response();
$response['Content-Type'] = 'text/html';
return new Template('main', function() {
return array('content' => new Template('error_404', null));
});
});
所以我覆盖了notFound函数来包装Closure并渲染它的返回值
首先我尝试使用较小的代码
public function notFound($callable = null) {
if (!is_null(($callable))) {
$this->router->notFound($this->auto_render_fun($callable));
} else {
Slim::notFound();
}
}
我也尝试这个(复制和修改旧代码)。
public function notFound($callable = null) {
if ( !is_null($callable) ) {
$this->router->notFound($this->auto_render_fun($callable));
// $this->router->notFound($callable); // old line
} else {
ob_start();
$customNotFoundHandler = $this->router->notFound();
if ( is_callable($customNotFoundHandler) ) {
call_user_func($customNotFoundHandler);
} else {
call_user_func(array($this, 'defaultNotFound'));
}
$this->halt(404, ob_get_clean());
}
}
但它之所以不起作用的原因是它的trow Slim_Exception_Stop
假设被Slim缓存在这里就是调用$this->notFound();
的代码行
https://github.com/codeguy/Slim/blob/master/Slim/Slim.php#L1160
它在try..catch里面。
这是堆栈跟踪(我在notFound函数中缓存它 - 但它应该在Slim类中处理)。
Slim_Exception_Stop in file libs/Slim/Slim/Slim.php at 862
0: Slim->stop() in libs/Slim/Slim/Slim.php at 882
1: Slim->halt(integer, string) in libs/System.php at 187
2: System->notFound() in libs/Slim/Slim/Slim.php at 1161
3: Slim->call() in libs/Slim/Slim/Middleware/Flash.php at 84
4: Slim_Middleware_Flash->call() in libs/Slim/Slim/Middleware/MethodOverride.php at 91
5: Slim_Middleware_MethodOverride->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
6: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
7: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Slim.php at 1098
8: Slim->run() in index.php at 573
答案 0 :(得分:0)
好的,我找到了原因,异常被处理了,但没有内容。我错误地认为它是Classes的东西,但只是Slim代码有bug。
public function halt( $status, $message = '' ) {
$this->cleanBuffer();
$this->response->status($status);
$this->response->body($message);
$this->stop();
}
函数覆盖您在处理程序中使用的数据,如
$app->notFound(function() {
$app->response()->body('Error');
});
不起作用,找不到的功能应该是这样的
public function notFound($callable = null) {
if (!is_null(($callable))) {
$this->router->notFound($this->auto_render_fun($callable));
} else {
$customNotFoundHandler = $this->router->notFound();
if (is_callable($customNotFoundHandler)) {
call_user_func($customNotFoundHandler);
} else {
call_user_func(array($this, 'defaultNotFound'));
}
$this->response->status(404);
$this->stop();
}
}