CodeIgniter 2.1 issue with show_404() and 404_override

13,195

Solution 1

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

Solution 2

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');  
}
Share:
13,195
Casper O
Author by

Casper O

Updated on June 13, 2022

Comments

  • Casper O
    Casper O almost 2 years

    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.

  • Casper O
    Casper O over 12 years
    Thank you for your response. first of all i heard that a redirect would be a bad idea, since it would not show the 404 on the correct page (instead all 404 would be shown as happening on example.com/404) which should be a bad idea. As you write, what i really want to is to be able to load views inside the 404 error to include my navigation, header and footer. SO editing the 404 template would be an option, if i could use views. I tried get_instance without any luck, found a blogpost about it but did't help (it was from 2008).
  • Casper O
    Casper O over 12 years
    Thank you so much. With that little code snippet helped me a lot. I even tried myself to load the controller i made to show my 404, but it seems like $CI->load-> is not supporting controllers. (The one I am using for 404_override). But this just made my day :)
  • landons
    landons over 12 years
    Maybe refactor your 404_override controller to simply call show_404(), and have the show_404() function load all the data you need. Keep it DRY man... ;)
  • Casper O
    Casper O over 12 years
    how would you do that? right now my 404_override goes to "frontpage/404" that just calls the same view as the above code do. I can't see the diffrent from writing $this->view('404') and show_404(); Except that it is smaller. I would still need to make a whole Controller just to do it.. Or you know something i don't :)
  • landons
    landons over 12 years
    Doesn't make a difference for a simple view. However, if you need a bunch of variables accessible in your view, you'll want to maintain that in the same place (the MY_Exceptions class).
  • Sparky
    Sparky over 11 years
    Something I was reading over at the CI forums... shouldn't this also include $CI->output->set_status_header('404'); for clean SEO purposes?
  • landons
    landons over 11 years
    Yes, it definitely should. Not just for SEO, but for general RESTfulness too.
  • CodeBrauer
    CodeBrauer over 9 years
    @Sparky - great! Thanks.
  • anupam
    anupam over 9 years
    No core hack please :)