giò*_*giò 19 php laravel laravel-5
我使用Laravel 5,我创建了一个文件404.blade.php中
views/errors/404.blade.php
Run Code Online (Sandbox Code Playgroud)
每次调用时都会呈现此文件:
abort(404); // alias of App::abort(404);
Run Code Online (Sandbox Code Playgroud)
如何传递自定义消息?像这样的东西404.blade.php
Sorry, {{ $message }}
Run Code Online (Sandbox Code Playgroud)
填写(示例):
abort(404, 'My custom message');
Run Code Online (Sandbox Code Playgroud)
要么
abort(404, array(
'message' => 'My custom message'
));
Run Code Online (Sandbox Code Playgroud)
在Laravel 4中,人们可以使用App::missing:
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.404', $data, 404);
});
Run Code Online (Sandbox Code Playgroud)
Dis*_*oat 37
在Laravel 5中,您可以为/resources/views/errors目录中的每个响应代码提供Blade视图.例如,将使用404错误/resources/views/errors/404.blade.php.
手册中未提及的是视图内部您可以访问该$exception对象.因此,您可以使用它{{ $exception->getMessage() }}来获取您传递的消息abort().
使用您自己的方法扩展Laravel的Exception Handler,Illuminate\Foundation\Exceptions\Handler和覆盖renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)方法.
如果你还没跑php artisan fresh,那对你来说很容易.只需编辑app/Exceptions/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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,$e在你的中使用变量404.blade.php.
即
abort(404, 'Something not found');
Run Code Online (Sandbox Code Playgroud)
在你的 404.blade.php
{{ $e->getMessage() }}
Run Code Online (Sandbox Code Playgroud)
对于其他有用的方法getStatusCode(),请参阅Symfony\Component\HttpKernel\Exception
| 归档时间: |
|
| 查看次数: |
12473 次 |
| 最近记录: |