2017-07-17 49 views
-1

我想弄清楚如何讓Silex中的錯誤處理程序呈現一個樹枝模板。這是他們在文檔中提供:在Silex中使用錯誤處理程序時,如何呈現樹枝模板?

$app->error(function (\Exception $e, Request $request, $code) { 
    return new Response('We are sorry, but something went terribly wrong.'); 
}); 

我寫的是:

$app->error(function (\Exception $e, Request $request, $code) { 
    return $app['twig']->render('error.twig'); 
}); 

我ASLO嘗試:

$app->error(function (\Exception $e, Request $request, $code) { 
    return new Response($app['twig']->render('error.twig')); 
}); 

我找不到,通過這些方法去手動我本可以在Silex及其錯誤處理方面工作。

回答

1

變量app不知道里面關閉,你需要告訴closureuse它。這樣你將可以訪問twig,你可以渲染一個模板。

$app->error(function (\Exception $e, Request $request, $code) use($app) { 
    return $app['twig']->render('error.twig'); 
});