我想为4xx http状态代码(例如404、400…)创建一个自定义错误页面。但我需要整个$app上下文,因为我想显示我的页眉、页脚和搜索字段。
我在app/start/global.php文件中读了很多关于自定义解决方案的内容,但这并没有帮助。这对5xx状态代码很好,因为我不需要$app上下文(在大多数情况下,如果有500,我无论如何都无法访问)。
这对于5xx:来说很好
App::error(function(Exception $exception, $code)
{
if (App::isDownForMaintenance()) {
return Response::make("Maintenance, brb.", 503);
}
if (Config::get('app.debug')) return;
switch ($code)
{
case 500: /* internal error */
default:
return View::make('_layouts.error_500');
}
});
但是,我如何在Laravel中使用$app创建4xx自定义页面?
我的Laravel版本是:4.2.x
您可以使用App::missing()
:
App::missing(function($exception)
{
$app = app();
return Response::view('errors.missing', compact('app'), 404);
});
更多信息:http://laravel.com/docs/4.2/errors#handling-404错误