是否可以在laravel中为后端和前端创建单独的错误页面?

问题描述 投票:0回答:1

我目前在我具有公众视野的项目中工作,即出于管理目的的前端和后端。

当遇到404错误时,我想为后端和前端显示单独的错误页面。

在laravel中可以做吗?

还是我们也可以基于名称空间创建错误页面?

目前,我在/resources/views/errors/目录中有错误页面。

任何建议表示赞赏。 如果需要更多信息,请随时询问。

php laravel exception-handling
1个回答
0
投票

可能不是最佳选择。

您可以根据异常处理程序的render()方法中的当前路径来分离视图。

应用程序/例外/ Handler.php:

public function render($request, Exception $exception)
{
    if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
        if(str_is('/admin*', request()->path())){
            return response()->view('errors.backend.404', [], 404);
        } else {
            return response()->view('errors.frontend.404', [], 404);
        }
    }

    return parent::render($request, $exception);
}
© www.soinside.com 2019 - 2024. All rights reserved.