博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用 Laravel 5.5+ 更好的来实现 404 响应
阅读量:7090 次
发布时间:2019-06-28

本文共 2253 字,大约阅读时间需要 7 分钟。

译文首发于 ,转载请注明出处!

Laravel 5.5.10 封装了两个有用的路由器方法,可以帮助我们为用户提供更好的 404 页面。现在,当抛出 404 异常时,Laravel 会显示一个漂亮的 404.blade.php 视图文件,你可以自定义显示给用户 UI,但在该视图中,你无权访问 sessioncookie,身份验证(auth)等...

在 laravel 5.5.10 中,我们有一个新的 Route::fallback() 方法,用于定义当没有其他路由与请求匹配时 Laravel 回退的路由。

Route::fallback(function () {    return 'Sorry' . auth()->user()->name . '! This page does not exist.';});

所以,现在我们可以使用具有正常页面和页脚的应用布局,来替代简单的 404 视图,同时还能给用户显示一条友好的提示信息。

Route::fallback(function() {    return response()->view('notFound', [], 404);});
@extends('layout.app')@section('content')    

Sorry! this page doesn't exist.

@stop

当 Laravel 渲染这个回退(fallback)路由时,会运行所有的中间件,因此当你在 web.php 路由文件中定义了回退路由时,所有处在 web 中间件组的中间件都会被执行,这样我们就可以获取 session 数据了。

API 接口说明

现在当你点击 /non-existing-page 时,你会看到在回退路由中定义的视图,甚至当你点击 /api/non-existing-endpoint 时,如果你也不想提供这个接口,你可以到 api 回退路由中定义 JSON 响应,让我们到 api.php 路由文件中定义另外一个回退路由:

Route::fallback(function() {    return response()->json(['message' => 'Not Found!]);});

由于 api 中间件组带有 /api 前缀,所有带有 /api 前缀的未定义的路由,都会进入到 api.php 路由文件中的回退路由,而不是 web.php 路由文件中所定义的那个。

使用 abort(404) 和 ModelNotFound 异常

当使用 abort(404) 时会抛出一个 NotFoundHttpException,此时处理器会为我们渲染出 404.blade.php 视图文件,同样的 ModelNotFoundException 异常也会做同样的处理,那么我们应该如何如何处理才能在更好的渲染出回退路由的视图,而不是一个普通的视图呢?

class Handler extends ExceptionHandler{    public function render($request, Exception $exception)    {        if ($exception instanceof NotFoundHttpException) {            return Route::responseWithRoute('fallback');        }        if ($exception instanceof ModelNotFoundException) {            return Route::responseWithRoute('fallback');        }        return parent::render($request, $exception);    }}

Route::respondWithRoute('fallback') 回去跑名为 fallback 的路由,我们可以像下面这样为回退路由命名:

Route::fallback(function() {    return response()->view('notFound', [], 404);})->name('fallback');

甚至,你还可以为特定的资源指定回退路由:

if ($exception instanceof ModelNotFoundException) {    return $exception->getModel() == Server::class                ? Route::respondWithRoute('serverFallback')                 : Route::respondWithRoute('fallback');}

现在我们需要在路由文件中定义这个回退路由:

Route::fallback(function(){    return 'We could not find this server, there are other '. auth()->user()->servers()->count() . ' under your account ......';})->name('serverFallback');

原文

你可能感兴趣的文章
JavaScript 解决 onblur 与 onclick 冲突
查看>>
cocos2d-x 事件分发机制 ——加速计事件监听
查看>>
物理层
查看>>
计算机图形学 椭圆的扫描转换(3)
查看>>
食物链 poj 1182
查看>>
oracle 11g impdp时 报ORA-12899(转)
查看>>
大型网站架构演变和知识体系
查看>>
vi 替换
查看>>
实例37foreach遍历数组
查看>>
性能测试
查看>>
js滚动到底部事件
查看>>
Newtonsoft.Json 用法
查看>>
Git和Repo管理使用简要介绍
查看>>
10-12-顺序表地址排序-内部排序-第10章-《数据结构》课本源码-严蔚敏吴伟民版...
查看>>
nbtstat Linux版源码, 通过IP获取主机名
查看>>
React Native——我的学习套路
查看>>
WinForm------窗体初始化位置的显示
查看>>
Asp.Net验证码1
查看>>
个人记录-虚拟现实
查看>>
python报错问题解决:'ascii' codec can't encode character
查看>>