在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
问题解决了,我的更新代码:
return response()->view('errors.missing', compact('exception'), 404);