将自定义消息(或任何其他数据)传递给Laravel 404.blade.php [英] Pass a custom message (or any other data) to Laravel 404.blade.php
问题描述
我正在使用Laravel 5,并且在其中创建了文件404.blade.php
I am using Laravel 5, and I have created a file 404.blade.php
in
views/errors/404.blade.php
此文件在我每次致电时都会呈现:
This file gets rendered each time I call:
abort(404); // alias of App::abort(404);
如何传递自定义消息? 404.blade.php
How can I pass a custom message? Something like this in 404.blade.php
Sorry, {{ $message }}
填写(例如):
abort(404, 'My custom message');
或
abort(404, array(
'message' => 'My custom message'
));
在Laravel 4中,可以使用App::missing
:
In Laravel 4 one could use App::missing
:
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.404', $data, 404);
});
推荐答案
(注意:从我的回答此处复制. )
在Laravel 5中,您可以为/resources/views/errors
目录中的每个响应代码提供Blade视图.例如,一个404错误将使用/resources/views/errors/404.blade.php
.
In Laravel 5, you can provide Blade views for each response code in the /resources/views/errors
directory. For example a 404 error will use /resources/views/errors/404.blade.php
.
手册中未提到的是,在视图内部您可以访问$exception
对象.因此,您可以使用{{ $exception->getMessage() }}
来获取传递给abort()
的消息.
What's not mentioned in the manual is that inside the view you have access to the $exception
object. So you can use {{ $exception->getMessage() }}
to get the message you passed into abort()
.
这篇关于将自定义消息(或任何其他数据)传递给Laravel 404.blade.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!