将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29163564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass a custom message (or any other data) to Laravel 404.blade.php
提问by giò
I am using Laravel 5, and I have created a file 404.blade.php
in
我使用Laravel 5,我创建了一个文件404.blade.php
中
views/errors/404.blade.php
This file gets rendered each time I call:
每次调用时都会渲染此文件:
abort(404); // alias of App::abort(404);
How can I pass a custom message? Something like this in 404.blade.php
如何传递自定义消息?像这样的东西404.blade.php
Sorry, {{ $message }}
Filled by (example):
填写人(示例):
abort(404, 'My custom message');
or
或者
abort(404, array(
'message' => 'My custom message'
));
In Laravel 4 one could use App::missing
:
在 Laravel 4 中可以使用App::missing
:
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.404', $data, 404);
});
回答by DisgruntledGoat
(Note: copied from my answer here.)
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
.
在 Laravel 5 中,您可以为/resources/views/errors
目录中的每个响应代码提供 Blade 视图。例如 404 错误将使用/resources/views/errors/404.blade.php
.
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()
.
手册中没有提到的是在视图中您可以访问$exception
对象。因此您可以使用{{ $exception->getMessage() }}
来获取您传入的消息abort()
。
回答by Harsh Vakharia
Extend Laravel's Exception Handler, Illuminate\Foundation\Exceptions\Handler
, and override renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)
method with your own.
扩展 Laravel 的异常处理程序,Illuminate\Foundation\Exceptions\Handler
并renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)
用您自己的方法覆盖方法。
If you haven't run php artisan fresh
, it will be easy for you. Just edit app/Exceptions/Handler.php
, or create a new file.
如果你还没有跑过php artisan fresh
,那对你来说会很容易。只需编辑app/Exceptions/Handler.php
,或创建一个新文件。
Handler.php
处理程序
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
class Handler extends ExceptionHandler {
// ...
protected function renderHttpException(HttpException $e) {
$status = $e->getStatusCode();
if (view()->exists("errors.{$status}")) {
return response()->view("errors.{$status}", compact('e'), $status);
}
else {
return (new SymfonyDisplayer(config('app.debug')))->createResponse($e);
}
}
}
And then, use $e
variable in your 404.blade.php
.
然后,$e
在您的404.blade.php
.
i.e.
IE
abort(404, 'Something not found');
and in your 404.blade.php
并且在你的 404.blade.php
{{ $e->getMessage() }}
For other useful methods like getStatusCode()
, refer Symfony\Component\HttpKernel\Exception
对于其他有用的方法,例如getStatusCode()
,请参阅Symfony\Component\HttpKernel\Exception
回答by Luceos
How about sharing a variable globally?
全局共享一个变量怎么样?
view()->share('message', 'llnk has gone away');
// or using the facade
View::share('message', 'llnk has gone away badly');
Just make sure in the template to fallback to a default in case you forget to set it.
只需确保在模板中回退到默认值,以防您忘记设置它。
See sharing data with views: http://laravel.com/docs/5.0/views
请参阅与视图共享数据:http: //laravel.com/docs/5.0/views