0

我对Slim 框架有这个问题。我有具有渲染方法的类模板,如果路由处理程序返回,我希望 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());
    }
}

但它不起作用的原因是它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
4

1 回答 1

0

好的,我找到了原因,异常处理了,但没有内容。我错误地认为它与类有关,但只是 Slim 代码有错误。

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();
    }
}
于 2012-08-24T21:19:45.870 回答