草庐IT

php - CodeIgniter 没有加载扩展的 MY_Exceptions

coder 2024-06-05 原文

我正在尝试覆盖 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() 时出现两个 fatal error

Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196

Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196

我还有其他工作良好的扩展类,具有相同的前缀 MY_

EDIT After @Tpojka suggestion my code is

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 错误,而不是简单的加载 View 。

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 打开了我的思路。

最佳答案

试试这个

<?
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->load->view('header')$this->load->view('footer') 放在 custom404.php 文件本身。在文件 custom404.php 文件中只回显 $heading$message

关于php - CodeIgniter 没有加载扩展的 MY_Exceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38833409/

有关php - CodeIgniter 没有加载扩展的 MY_Exceptions的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  7. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  8. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  9. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  10. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

随机推荐