子文件夹Codeigniter中的自定义404页面

时间:2015-01-08 06:11:59

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

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

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

有什么想法吗?感谢

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

您在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是否有路由

答案 2 :(得分:0)

只需查看您的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