Laravel 5:在视图中从Exception获取自定义消息

问题描述 投票:1回答:1

在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

renderHandler.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

laravel laravel-5
1个回答
0
投票

问题解决了,我的更新代码:

return response()->view('errors.missing', compact('exception'), 404);
© www.soinside.com 2019 - 2024. All rights reserved.