【问题标题】:custom 404 page in subfolder Codeigniter子文件夹 Codeigniter 中的自定义 404 页面
【发布时间】:2015-03-06 04:56:48
【问题描述】:

如何在子文件夹 Codeigniter 中创建自定义 404 页面? 我尝试了以下但无济于事:

http://tutsnare.com/create-custom-404-page-codeigniter/

有什么想法吗?谢谢

【问题讨论】:

    标签: codeigniter http-status-code-404 codeigniter-routing


    【解决方案1】:

    您是否在子文件夹中使用控制器?如果是这样,请通过创建 application/core/MY_Exceptions.php 来扩展 Exceptions 类

    输入以下代码:

    class MY_Exceptions extends CI_Exceptions 
    {
    
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * 404 Page Not Found Handler
         *
         * @access  private
         * @param   string
         * @return  string
         */
        public function show_404($page = '', $log_error = TRUE)
        {
    
            /**
             * Quick fix for 404 Override bug.
             * 
             * index.php/non-existent-controller                        -----> override ok
             * index.php/existent-controller/non-existent-method        -----> override not ok
             * index.php/existent-folder/non-existent-controller        -----> override not ok
             */
    
            $router =& load_class('Router', 'core');
            if ( ! empty($router->routes['404_override']))
            {
                if ($log_error) log_message('error', '404 Page Not Found --> '. $page);
    
                $config =& load_class('Config', 'core');
                header('Location: '. $config->site_url($router->routes['404_override']));
                exit;
            }
    
            /* End of fix */
    
            $heading = "404 Page Not Found";
            $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', '404 Page Not Found --> '.$page);
            }
    
            echo $this->show_error($heading, $message, 'error_404', 404);
            exit;
        }
    }
    

    【讨论】:

    • 它正在工作。但是 URL 正在改变。这是因为 header()。有没有办法保留用户输入的原始(断开的链接)?
    • 您可以将 header() 更改为 echo file_get_contents($config->site_url($router->routes['404_override']));
    【解决方案2】:

    您的 route.php 配置中有 $route[‘404_override’] = ‘’;。 您可以设置一个类似 page_error.php 的控制器并保留 URL,这样您就不会有重定向。

    $route['404_override'] = 'page_error'; 
    
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class page_error extends CI_Controller
    {
     function index()
      {
        $this->output->set_status_header('404');
         echo '404';
       }
    } 
    ?>
    

    检查here 的路由

    【讨论】:

      【解决方案3】:

      只需查看您的 application/routes.php 文件$route['404_override'] = '';

      首先将$route['404_override'] = '';以上更改为$route['404_override'] = 'custom_error';

      去你的应用程序/控制器

      创建名为 custom_error 的新控制器

      class Custom_error extends CI_Controller
      {
          public function __construct()
          {
              parent::__construct();
      
      
      
          }
      
          public function index()
          {
              echo "404 Rajaram Custom Message";
          }
      
      }
      

      想了解更多路线详情请访问官网
      https://ellislab.com/codeigniter/user-guide/general/routing.html

      【讨论】:

      猜你喜欢
      • 2011-09-17
      • 2015-01-14
      • 2011-12-23
      • 2019-01-05
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 2012-06-13
      相关资源
      最近更新 更多