【问题标题】:Laravel doesn't show custom Blade view for HTTP status 403Laravel 不显示 HTTP 状态 403 的自定义 Blade 视图
【发布时间】:2019-01-25 13:58:16
【问题描述】:

根据 Laravel 的official docs,您可以通过将所述视图添加到资源/视图/错误并将文件命名为 [httpcode].blade.php 来显示给定 HTTP 响应的自定义 Blade 视图。我已经为 404 错误执行了此操作,每当遇到 404 错误时,Laravel 都会显示此视图。但是,当遇到 403 错误时,Laravel 会显示默认的、丑陋的 Apache 403“禁止”错误页面。

我的“大师”刀片布局(master.blade.php):

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="description" content="SQLess is a database management application.">
    <title>{{ $title }}</title>
    <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}">
    @stack('css-extras')
    @stack('scripts-extras')
</head>
<body>
@yield('content')
</body>
</html>

我的自定义 403 布局(403.blade.php):

@extends('master', ['title' => "Forbidden"])
@section('content')
    <div>You shouldn't be here. This is a 403 error!</div>
@endsection

为什么这不起作用?

【问题讨论】:

  • 检查您的apache logs 以确保它不是 apache 正在处理的实际目录权限问题。
  • @ourmandave Apache 日志显示:[Sat Aug 18 22:37:33.078666 2018] [autoindex:error] [pid 7688:tid 1952] [client ip:51179] AH01276: Cannot serve directory C:/xampp/htdocs/sqless/public/img/: No matching DirectoryIndex (index.php,index.pl,index.cgi,index.asp,index.shtml,index.html,index.htm,default.php,default.pl,default.cgi,default.asp,default.shtml,default.html,default.htm,home.php,home.pl,home.cgi,home.asp,home.shtml,home.html,home.htm) found, and server-generated directory index forbidden by Options directive 下一步是什么?

标签: php laravel


【解决方案1】:

Laravel 无法处理这个 403 错误。在这种情况下,Laravel 甚至不处理该请求。

根据您的 apache 配置,如果存在与请求的 URI 匹配的现有文件或目录,它将被提供。如果没有文件/目录,请求将被转发给 Laravel 处理。这是在 Laravel .htaccess 文件中定义的默认 Laravel 配置。

在您的情况下,目录 img 存在于 DocumentRoot 中,并且由 Apache 提供服务。您会看到 403 错误,因为该目录没有 index 文件。

所以,如果你想自定义这个 403 错误页面,你必须通过编辑 Apache 配置来实现。

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 2019-08-13
    • 2017-10-07
    • 1970-01-01
    • 2018-04-25
    • 2015-12-16
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多