如何使用 Codeigniter 在自定义 404 页面中重定向 404 错误? [英] How can I redirect a 404 Error in a custom 404 page using Codeigniter?

查看:44
本文介绍了如何使用 Codeigniter 在自定义 404 页面中重定向 404 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好心的先生们,我正在使用 Codeigniter 构建博客.我可能需要一种将 404 错误重定向到自定义 404 页面的方法.就像 Abduzeedo.com 的 404 页面一样.是否可以通过使用路由来控制它?或者我应该使用控制器将它定向到另一个视图?非常感谢!

Kind sirs, I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!

推荐答案

我使用了另一种方法:通过覆盖 Codeigniter 的 Exception 核心类.首先确保您的配置文件(system/application/config/config.php)子类前缀如下$config['subclass_prefix'] = 'MY_';

there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following $config['subclass_prefix'] = 'MY_';

然后在 system/application/libraries 中创建一个名为 MY_Exceptions.php 的文件.然后在此处覆盖函数 show_404() 函数,如下所示.

Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.

class MY_Exceptions extends CI_Exceptions{
    function MY_Exceptions(){
        parent::CI_Exceptions();
    }

    function show_404($page=''){    

        $this->config =& get_config();
        $base_url = $this->config['base_url'];

        $_SESSION['error_message'] = 'Error message';
        header("location: ".$base_url.'error.html');
        exit;
    }
}

现在错误控制器将是您的错误页面,该页面将被重定向到 404 错误.

Now Error controller will be your error page, where the page will be redirected for 404 error.

这篇关于如何使用 Codeigniter 在自定义 404 页面中重定向 404 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆