在Laravel 5中我的代码为:
$data = Contents::whereSlug($content)->orWhere('id', '=', $content)->first();
可能会返回null
。我想用自定义消息管理它并在视图中显示。例如:
Route::get('/showContent/{content}', function ($content) {
$data = Contents::whereSlug($content)->orWhere('id', '=', $content)->first();
if ($data == null) {
abort(404, 'content not found');
}
$serverInformation = ServerInformation::find(1);
return view('layouts.frontend.pages.showContent', compact(
'data',
'serverInformation'
));
});
我在errors
上有resources/views/missing.blade.php
个文件夹,现在我希望此代码在content not found
上显示此消息:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>{{ $exception->getMessage() }}</h2></body>
</html>
不幸的是我收到了这个错误:
Undefined variable: exception (View: /Applications/XAMPP/xamppfiles/htdocs/alachiqServer/resources/views/errors/missing.blade.php
在render
文件上和Handler.php
:
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException or $e instanceof NotFoundHttpException) {
if ($request->ajax()) {
return response()->json(['error' => 'Not Found'], 404);
}
return response()->view('errors.missing', [], 404);
}
return parent::render($request, $e);
}
我该如何解决这个问题? resource
答案 0 :(得分:0)
问题解决了,我的更新代码:
return response()->view('errors.missing', compact('exception'), 404);