首页 > 解决方案 > 如何让我的 Yii2 404 页面显示在实时服务器上

问题描述

我在 Yii 安装的 view 文件夹中自定义了404.php页面。在本地服务器上非常有效,显示网站页眉和页脚以及404.php页面上的自定义内容。

但是,当我上传到实时服务器时,找不到页面时不会显示 404.php 页面,而是显示带有消息 - Sorry! This page does not exist- 的纯白色页面。我怎样才能解决这个问题?

显然,view/404.php 没有被使用。我的error.php的内容如下:

<?php

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

use yii\helpers\Html;

$this->title = $name;
?>
                <!----Section Title Starts---->
                <div class="article-title-wrapper uk-section uk-section-primary">
                    <div class="uk-container">

                        <div class="uk-width-5-6@l uk-margin-auto">

                            <div class="uk-flex uk-flex-center">
                                <div class="section-intro uk-width-1-1 uk-width-2-3@l uk-text-center" uk-scrollspy="target: > div; cls:uk-animation-fade; repeat: true">
                                    <div class="page-title">
                                        <h1 class="uk-article-title">Page Not Found</h1>
                                    </div>
                                </div>
                            </div>
                        </div>          
                    </div>
                </div>  
                <!----Section Title ends---->

                <!----Section Content Starts---->
                <div class="article-content uk-section uk-section-default uk-padding-remove-bottom">
                    <div class="uk-container">
                        <div class="left-indent">
                            <div class="uk-width-5-6@l uk-margin-auto">
                                <div class="uk-flex uk-flex-center">
                                    <div class="section-content uk-width-1-1">
                                        <article class="uk-article uk-margin-large-bottom uk-text-center">
                                            <p class="uk-text-large">Sorry, but the page you were trying to view does not exist.</p>
                                        </article>
                                    </div>
                                </div>

                                <hr class="double">                     
                            </div>
                        </div>                      
                    </div>
                </div>  
                <!----Section Content ends---->

我已经检查了这里给出的答案,但它确实解决了我的问题。我没有更改显示错误的默认配置。根据 Yii2 的默认配置将使用 - YOUR_APP/views/site/error.php 来显示错误。这适用于我的本地服务器,但不适用于我的问题的实时服务器。

更新:我有两个互联网连接。我切换了互联网连接,突然发现 404 页面正常出现。我切换回另一个连接,问题又回来了。我来回切换,很明显,由于未知原因,我可以在一个互联网连接上获得正确的 404 页面,而在另一个互联网连接上却无法正常工作。这似乎不正常。

然后我在互联网连接上使用了一个 VPN,它没有带来正确的 404 页面,它也运行良好。是否有拒绝某些类型连接的安全功能?

标签: phpyii2yii2-basic-app

解决方案


您需要在web/index.php中设置以下内容或高级应用程序浏览到frontend/web/index.php

defined('YII_DEBUG') or define('YII_DEBUG', false);

或者

您可以使用状态码引发自定义错误。例如,

throw new \yii\web\NotFoundHttpException(status code);

推荐阅读