CodeIgniter Forums
Problem with 404_override - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with 404_override (/showthread.php?tid=42525)

Pages: 1 2


Problem with 404_override - El Forum - 06-09-2011

[eluser]mcwilly[/eluser]
I recently upgraded my CI version to 2.0 and tried using the

Code:
$route['404_override'] = 'error';


and it looked like it had a common issue of working only if the controller was not found, but if a function didnt exist in a controller, you got the default 404 page. Today i upgraded to 2.0.2 and I see it is 'kinda' working for me now. It will take me to the correct controller, but it does not autoload anything. If i try to manually load something, that doesnt work either. For instance if i try to load a modal as I do on any other controller, it doesnt find it

Code:
$this->load->model('ad_model');

results in this

Quote:Severity: Notice

Message: Undefined property: Error::$ad_model

Filename: controllers/error.php

Line Number: 24

This also applies to when i try and call any kind of session variable. Any ideas on why this is happening

Thanks


Problem with 404_override - El Forum - 06-09-2011

[eluser]toopay[/eluser]
Thats because at "404" state, any CI resource is cleaned. So theres no chance you can access session, model, and other CI resource. And anyway, why you need to process something, when user clearly try to access : NOTHING!


Problem with 404_override - El Forum - 06-09-2011

[eluser]mcwilly[/eluser]
[quote author="toopay" date="1307691162"]Thats because at "404" state, any CI resource is cleaned. So theres no chance you can access session, model, and other CI resource. And anyway, why you need to process something, when user clearly try to access : NOTHING![/quote]

I am taking the user to a Site map type page, and on that page there are some common areas that use the session. And if thats the case, then why is it that when the 404 override is accessed when a controller does not exist, the same page works fine.


Problem with 404_override - El Forum - 06-09-2011

[eluser]toopay[/eluser]
At 404 HTTP state, you should do nothing other than told user that they try to access a resource which not exist. Theres no need to process a session, or furthermore, a database, for this. For example, look at https://foursquare.com/foo=bar source, or even check their cookies in some web/http debugger tool, and you will found nothing except just a page with a simple message : "We couldn't find the page you're looking for".


Problem with 404_override - El Forum - 06-09-2011

[eluser]mcwilly[/eluser]
i understand what your saying, but i still dont get why www.mysite.com/existing_controller/bad_function does not work, and www.mysite.com/non_existing_controller does. I have both of these scenarios going to the same 404 override page accessing the same resources.


Problem with 404_override - El Forum - 06-18-2011

[eluser]MILL[/eluser]
[quote author="mcwilly" date="1307696646"]i understand what your saying, but i still dont get why www.mysite.com/existing_controller/bad_function does not work, and www.mysite.com/non_existing_controller does. I have both of these scenarios going to the same 404 override page accessing the same resources.[/quote]

I have the same situation as mcwilly and completely agree with him.

[quote author="toopay" date="1307696203"]you should do nothing other than[/quote]
Why "should" I change my user-friendly interface to some scary page without navigation, etc?
But for navigation I use libraries, models...

I guess ist's quite simple to load 2-nd controller, then "remap" missing first-level "methods".

I get an old CodeIgnitor's trick: CI_Model::_assign_libraries() method and change lines in CodeIgniter.php from:
Code:
unset($CI);
$CI = new $class();
to:
Code:
$CI2 = new $class();
                    
                foreach (array_keys(get_object_vars($CI)) as $key)
                {
                    if ( ! isset($CI2->$key) )
                    {            
                        $CI2->$key = NULL; // Needed to prevent reference errors with some configurations
                        $CI2->$key =& $CI->$key;
                    }
                }
                unset($CI);
                $CI = clone($CI2);
                unset($CI2);

Voila! All works now.


Problem with 404_override - El Forum - 06-23-2011

[eluser]SneakyDave[/eluser]
This 404 override is pretty worthless in my opinion. The problem is that some people may want to simply override the 404 processing by using the main controller of the web site.

This is pretty much worthless if you have any libraries auto loaded because the 404 override won't load any libraries, so the first time a library method is referenced, the user will see a PHP error, or a blank page, depending on the ENVIRONMENT.

The way I fixed it for a particular client that wanted the home page displayed for any "page not found" was to add some code in the index method of the default controller to check to see if the library object existed. If it didn't, that meant that the library wasn't auto-loaded, which means that came from a 404 error, so I force a redirect to default controller of the site, which would then function normally. A PITA, but you get what you pay for.

I don't use this anymore, because I think, it's sloppy, but there's got to be a better way. Forgive my use of the alternative IF/ENDIF syntax.

In the index method of the controller that is called by overriding the 404:
Code:
$this->_check_for_bad_object();

Code:
function _check_for_bad_object()
    {
        error_reporting(0);
        if(! is_object($this->the_library_that_should_be_autoloaded)):
            redirect('the real 404 redirect controller', 'refresh');
        endif;            
        switch (ENVIRONMENT)
        {
            case 'development':
                error_reporting(E_ALL);
            break;
    
            case 'testing':
            case 'production':
                error_reporting(0);
            break;
        }
}

You could probably use this for any 404 controller you wanted, as I think the only time a library file wouldn't be auto-loaded is a 404 condition, although you'd probably have to set your own header error value.


Problem with 404_override - El Forum - 06-24-2011

[eluser]MILL[/eluser]
[quote author="SneakyDave" date="1308895482"]
Code:
redirect('the real 404 redirect controller', 'refresh');
[/quote]

I think it is not so cool to populate a second request, especially on high loaded services.


Problem with 404_override - El Forum - 06-24-2011

[eluser]toopay[/eluser]
I still think that 404 or other HTTP error, should not give anything excepts simple error message. Its actually better already, rather than Apache 404 error. Because, theoritically, sends 200 OK at HTTP header response while user should receive 404 Not Found, its a wrong approach.


Problem with 404_override - El Forum - 06-24-2011

[eluser]SneakyDave[/eluser]
I don't disagree with you. My point is that the override feature was added for people to use. but it obviously wasn't tested. It really won't work for anybody that would use an auto-loaded library in their 404 controller, and it doesn't look like you can manually load it either.

They should fix it or remove the feature.