【问题标题】:CodeIgniter not loading extended MY_ExceptionsCodeIgniter 未加载扩展的 MY_Exceptions
【发布时间】:2016-08-08 15:41:31
【问题描述】:

我正在尝试从 CI_Exceptions 覆盖 show_404 方法,但从未加载过 MY_Exceptions

MY_Exceptions 位于 application/core/

代码

<?php
class MY_Exceptions extends CI_Exceptions {
    function show_404(){
        header("HTTP/1.1 404 Not Found");
        $CI =& get_instance();
        $CI->load->view('header.php');
        $CI->load->view('err/custom404.php');
        $CI->load->view('footer.php');
    }
}

当我打电话给show_404() 时,我收到两个致命错误

致命错误:在第 196 行的 C:\workspace\ictp-tv-main\system\core\Common.php 中找不到类“MY_Exceptions”

致命错误:在第 196 行的 C:\workspace\ictp-tv-main\system\core\Common.php 中找不到类“MY_Exceptions”

我还有其他运行良好的扩展类,前缀相同MY_

编辑 在@Tpojka 建议后,我的代码是

class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {
        parent::__construct();
        $this->CI =& get_instance();
    }

    public function show_404($page = '', $log_error = TRUE){

        header("HTTP/1.1 404 Not Found");

        $this->CI->load->view('header.php');
        $this->CI->load->view('err/custom404.php');
        $this->CI->load->view('footer.php');

    }
}

现在 404 页面是空白的。

编辑

解决方案

我们需要 ECHO 错误,而不是简单的加载视图。

public function show_404($page = '', $log_error = TRUE){
    header("HTTP/1.1 404 Not Found");
    echo $this->CI->load->view('header.php',array('PAGE_TITLE'=>'Page not found'),true);
    echo $this->CI->load->view('err/custom404.php',null,true);
    echo $this->CI->load->view('footer.php',null,true);
    exit(4);
}

感谢@Tpojka 打开我的思路。

【问题讨论】:

    标签: php windows codeigniter


    【解决方案1】:

    试试这个方法

    <?
    class MY_Exceptions extends CI_Exceptions
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function show_404($page = '', $log_error = TRUE)
        {
            if (is_cli())
            {
                $heading = 'Not Found';
                $message = 'The controller/method pair you requested was not found.';
            }
            else
            {
                $heading = '404 Page Not Found This Time';
                $message = 'The page you requested was not found.';
            }
            // By default we log this, but allow a dev to skip it
            if ($log_error)
            {
                log_message('error', $heading.': '.$page);
            }
            echo $this->show_error($heading, $message, 'custom404', 404);//custom404 is in APPPATH.'views/errors/html/custom404.php'
            exit(4); // EXIT_UNKNOWN_FILE
        }
    }
    

    我在上面编辑了代码。我进行了研究并以此结束。我刚刚从 CI_Exceptions 类中复制了show_404() 方法,并查看了可以进行哪些更改。您可以设置您的消息、标题和调用您的模板。你可以这样玩。另外我建议您将$this-&gt;load-&gt;view('header')$this-&gt;load-&gt;view('footer') 放在custom404.php 文件本身中。在文件custom404.php 文件中只回显$heading$message

    【讨论】:

    • 我改变了这种方式,但得到了同样的错误。我发现 show_404 签名是不同的。现在调用了正确的方法,但显示了一个空白页。看起来视图是空的
    猜你喜欢
    • 2018-01-22
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2020-02-23
    • 2017-07-19
    相关资源
    最近更新 更多