【问题标题】:Custom view in show_404() not workingshow_404() 中的自定义视图不起作用
【发布时间】:2014-04-26 01:03:25
【问题描述】:

我已尝试使用 404_override 和 MY_Exceptions 来显示自定义错误视图。有人可以帮我吗?

这是 404_override 代码

Routes.php

$route['404_override'] = 'error/index';

错误.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Error extends CI_Controller {

    public $user_id ='';

    function __construct()
    {
        parent::__construct();
        $this->load->library('login.php');
        $user_Details = $this->login_library->get_user_details();
        $this->user_id = $user_Details['user_id'];
    }

    public function index()
    {
        $this->output->set_status_header('404');
         $this->output->set_status_header('404');  
        $this->load->view('view_header',$this->user_id); 
        $this->load->view('view_404');
        $this->load->view('view_footer',$this->user_id);     
    }



}

这是 My_Exceptions 代码

<?php  if (! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions{

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

    function show_404($page=''){ 
        echo $heading = "404 Page Not Found";
        echo $message = "The ppage you requested was not found.";
        $this->config =& get_config();
        $baseUrl = $this->config['base_url'];
        header("location: ".$baseUrl.'error.html');
        exit;
    }

}

【问题讨论】:

  • 不确定您要达到的目标,但这可能会有所帮助:stackoverflow.com/questions/21735851/…
  • 我认为这很有解释性。这是一个简单的问题。 $route['404_override'] 有什么办法吗?
  • 如果你想使用自定义的 404 错误页面,你不必扩展 CI_Exception 核心类,你的 Error 控制器对我来说似乎很好。在index() 方法中回显$this-&gt;user_id 以确保它有效。
  • 它不工作。我尝试了这两个东西,即 routes.php 和 my_exceptions 但我不好。我不知道我到底在哪里做错了。我应该尝试一个新的 CI 项目和 404_override 来检查它是我的其他项目的事情还是它是一个 CI 错误。
  • 它在我的版本中不起作用,但在新版本中起作用。谢谢。帮助表示赞赏。我还尝试了 show_404() 任何我想调用该错误函数的地方,但它不起作用。感谢您的帮助。

标签: codeigniter exception error-handling routing http-status-code-404


【解决方案1】:

我从这个链接得到了解决方案 - CodeIgniter 2.1 issue with show_404() and 404_override

<?php
// application/core/MY_Exceptions.php
class MY_Exceptions extends CI_Exceptions {

    public function show_404()
    {
        $CI =& get_instance();
        $CI->load->view('my_notfound_view');
        echo $CI->output->get_output();
        exit;
    }
}

非常感谢大家的帮助。赞赏:)

【讨论】:

  • 它没有。它会为自定义行为覆盖它,这是常见的做法。
【解决方案2】:

如果要显示自定义 404 模板

你需要改变 $route['404_override'] = 'welcome/page_not_found';在 config/routes.php 文件中。

在控制器中编写动作

public function page_not_found(){
    $this->data['page_title'] = 'Page Not Found!';
    $this->load->view('layouts/page_not_found.php', $this->data);
}

重要提示:您的 page_not_found 操作应该在您的默认控制器中 (您可以从 config/routes.php 设置默认控制器,$route['default_controller'] = 'welcome';)

【讨论】:

    【解决方案3】:

    尝试把config.php改成

    $config['uri_protocol'] = "REQUEST_URI";
    

    有用吗?

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题并尝试扩展 Exceptions 类,但 codegniter 不支持扩展 Exceptions 类

      这对我有用,以便覆盖 show_404() 方法

      创建一个辅助方法

      if ( ! function_exists('custom_show_404'))
      {
          /**
          * Method Name custom_show_404
          *
          */
          function custom_show_404()
          {
              $CI =& get_instance();
              $CI->load->view("not_found_page");
      
              //log message
              log_message('error','PAGE NOT FOUND ETC...');
          }
      }
      

      之后,在 application/controllers 文件夹中查找所有 show_404() 方法调用并将其替换为 custom_show_404()

      注意:不要忘记加载包含新方法的帮助文件

      【讨论】:

        【解决方案5】:

        我来不及回答这个问题,但也许这会帮助其他人寻找解决这个问题的方法。

        我尝试了Chopra 提出的answer 但它不起作用,因为show_404 签名必须与它覆盖的签名相同。

        我已将核心类 CI_Exceptions 替换为新的。

        <?php
        // application/core/MY_Exception.php
        class MY_Exceptions extends CI_Exceptions
        {
            /**
             * Show a custom 404 error
             */
            public function show_404($page = '', $log_error = TRUE)
            {
                $CI =& get_instance();
                $data['title'] = '404 Page Not Found!';
                $output = '';
                $output .= $CI->load->view('templates/header', $data, true);
                $output .= $CI->load->view('error_404', '', true);
                $output .= $CI->load->view('templates/footer', $data, true);
                set_status_header(404);
                echo $output;
                exit;
            }
        }
        

        【讨论】:

          【解决方案6】:

          您必须通过 show_404() 函数编辑 CodeIgniter 的核心文件才能使用自定义 404 页面。只需转到 \system\core\Common.php 并将 show_404() 函数替换为以下代码:

          function show_404($page = '', $log_error = TRUE)
          {
               $ci = get_instance();
               $ci->output->set_status_header(404);
               $ci->load->view('404');
               echo $ci->output->get_output();
               exit(4);
          }
          

          注意:请用您的自定义替换 Your_custom_404_page 文本 404 页面。

          它在我自己的 Codeigniter 应用程序上正常工作。我希望它能正常工作

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-21
            • 1970-01-01
            • 2020-10-12
            • 2020-01-18
            相关资源
            最近更新 更多