I'm new to Lumen and want to create an app with this framework. Now I have the problem that if some user enters a wrong url => http://www.example.com/abuot (wrong) => http://www.example.com/about (right), I want to present a custom error page and it would be ideal happen within the middleware level.

Furthermore, I am able to check if the current url is valid or not, but I am not sure how can I "make" the view within the middleware, the response()->view() won't work.

如果有人能帮忙,那就太棒了.

推荐答案

由于错误是App\Exceptions\Handler次处理的,所以这里是处理错误的最佳场所.

如果您只需要一个自定义的404错误页面,那么您可以很容易地做到这一点:

将此行添加到Handler文件的顶部:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

render函数改为:

public function render($request, Exception $e)
{
    if($e instanceof NotFoundHttpException){
        return response(view("errors.404"), 404);
    }
    return parent::render($request, $e);
}

这假设您的自定义404页面存储在视图中的错误文件夹中,并将返回自定义错误页面和404状态代码.

Laravel相关问答推荐

查询Laravel中的图形

laravel如何在Blade 模板中将元素添加到数组

Laravel Eloquent:组合列(或自定义属性)上的 Where 子句

assertSee 由于 html 代码中的空格而失败

使用 Laravel 计算页面浏览量

在laravel中将数据插入数据透视表

Blade引擎:打印三重花括号

Lumen和 MongoDB?

Laravel Request::input 调用未定义的方法

在 Laravel Eloquent 中,limit 与 take 有什么区别?

Laravel - 批量分配异常错误

升级到 Laravel 5.2 会使所有会话失效

在 Laravel 容器中覆盖单例

Laravel Eloquent 多态一对一?

Laravel 4:将什么作为参数传递给 Url 类?

Laravel 应用程序连接数据库时速度很慢

在 Laravel 中包含 bootstrap 程序

如何在 vs 代码中自动导入 laravel 类

是否可以在 Laravel 的不同数据库中引用外键?

Laravel 5.2 |测试 UploadedFile 在发布后错过了 $test 值?