CodeIgniter 2.1 的 show_404() 和 404_override 问题

发布于 2024-12-20 10:44:27 字数 330 浏览 2 评论 0原文

我已经使用 CodeIgniter 有一段时间了,目前我正在为需要自定义 404 页面的客户做一个项目。一切都很好,404_override 效果很好。

当访问者尝试访问不存在的文章时,我的问题出现了我想调用 show_404() 函数,但这向我显示了“开箱即用” 404页面而不是404_override中写入的页面。

我已经看到了旧版本的一些修复,但我无法让它在 2.1 中工作。因此,如果有人能帮助我解决这个问题,我将非常感激。

I have been working with CodeIgniter for quite a while now and I'm currently doing a project for a client that would like a custom 404 page. Everything is fine and the 404_override it works great.

My issue comes when a visitor is trying to access a article that do not exist I would like to call the show_404() function, but this shows me the "out of box" 404 page and not the one written in the 404_override.

I have seen some fixes for older versions, but I can't get it to work in 2.1. So if anyone can help me out of this I would be really grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み青杉依旧 2024-12-27 10:44:27

重定向是最干净的;加载视图也可以工作。

这里的问题是 show_404() 通常在控制器已经加载之后调用(毕竟必须有东西告诉它显示 404)。 CI 不喜欢在此时加载第二个控制器,这是主要障碍。

您最好的选择可能是扩展 Exceptions 类中的 show_404() 函数以重定向到您的 404 路由。如果没有重定向,您将不得不显示视图或您知道在调用 404 之前拥有所需的所有“额外数据”的内容(或者我猜您也可以将其加载到 Exceptions 类中)。在某些动态视图中它可能会变得非常复杂。

您显然想要一些更高级的东西,而不仅仅是编辑错误文件夹中的 404 模板。我在尝试从该文件访问 get_instance() 时遇到问题,因为有时它是在构造控制器之前加载的。因此,如果您尝试这样做,请务必小心;)

更新:这是扩展 show_404() 函数以加载视图的工作示例

<?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;
    }
}

Redirect is cleanest; loading a view could work too.

The problem here is that show_404() is usually called AFTER your controller has already loaded (something had to tell it show the 404 after all). CI doesn't like loading a second controller at that point, which is the primary hurdle.

Your best option probably is extending the show_404() function in the Exceptions class to redirect to your 404 route. Without a redirect, you'll be stuck with showing a view or something that you know has all the "extra data" it needs prior to calling the 404 (or I guess you could load it in the Exceptions class too). It can get really complicated in some dynamic views.

You obviously want something more advanced than just editing the 404 template in the errors folder. I've had problems trying to access get_instance() from that file, as sometimes it's loaded before the controller is constructed. So, be careful if you try that ;)

Update: Here's a working example of extending the show_404() function to load a view

<?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;
    }
}
海的爱人是光 2024-12-27 10:44:27

我解决问题的快速方法是在我正在处理的项目中的自动加载帮助程序文件中创建一个函数。这个助手只是重定向到自定义 404 页面。所以我只是调用 show_my_404() 而不是 show_404()

function show_my_404(){   
    //Using 'location' not work well on some windows systems
    redirect('controller/page404', 'location');  
}

My fast approach to solving the problem is to create a function in an autoloaded helper file in the project i'm working on. This helper just redirects to the custom 404 page. So i just call show_my_404() instead of show_404()

function show_my_404(){   
    //Using 'location' not work well on some windows systems
    redirect('controller/page404', 'location');  
}
云仙小弟 2024-12-27 10:44:27
  function show_404($page = '', $log_error = TRUE)
{
    if ($log_error)
    {
        log_message('error', '404 Page Not Found --> '.$page);
    }
    redirect(base_url().index_page().'/controller/error_page');
           //OR
           //redirect('SOME URL');

    exit;
}
  function show_404($page = '', $log_error = TRUE)
{
    if ($log_error)
    {
        log_message('error', '404 Page Not Found --> '.$page);
    }
    redirect(base_url().index_page().'/controller/error_page');
           //OR
           //redirect('SOME URL');

    exit;
}
烟柳画桥 2024-12-27 10:44:27

我正在寻找同样的东西,但发现了很长的解决方案,例如创建自己的错误控制器和异常类或简单地重定向到 404 页面(这在语义上不好)。

所以我对 CI 如何在内部调用“404_override”路由进行了一些研究。这是我想出的解决方案:

// Override show_404 method
function show_404(){
    // Load Router and Core classes.
    $RTR =& load_class('Router', 'core');

    // Get class and method for 404 from routes.php
    $x = explode('/', $RTR->routes['404_override']);
    $class = $x[0];
    $method = (isset($x[1]) ? $x[1] : 'index');

    // Get current class and method for callback
    $callback_class = $RTR->fetch_class();
    $callback_method = $RTR->fetch_method();
    // Can also log here, using callback class and method.

    // Create object for callback
    $CI = new $callback_class;
    call_user_func_array(array(&$CI, $method), array_slice(array($class,$method), 2));
} // End!

system/core/CodeIgniter.php 文件中添加此函数。
这将覆盖 show_404 函数。您可以从控制器调用它,它将调用routes.php 文件中给出的“404_override”路由。

I was searching for the same thing, but found quite long solutions like creating own error controller and exception class or simply redirecting to 404 page (which isn't good semantically).

So I did bit research on how CI calls '404_override' route internally. Here's the solution I came up with:

// Override show_404 method
function show_404(){
    // Load Router and Core classes.
    $RTR =& load_class('Router', 'core');

    // Get class and method for 404 from routes.php
    $x = explode('/', $RTR->routes['404_override']);
    $class = $x[0];
    $method = (isset($x[1]) ? $x[1] : 'index');

    // Get current class and method for callback
    $callback_class = $RTR->fetch_class();
    $callback_method = $RTR->fetch_method();
    // Can also log here, using callback class and method.

    // Create object for callback
    $CI = new $callback_class;
    call_user_func_array(array(&$CI, $method), array_slice(array($class,$method), 2));
} // End!

Add this function in system/core/CodeIgniter.php file.
This will override show_404 function. You can call it from your controllers and it will call '404_override' route given in routes.php file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文