Welcome Guest, Not a member yet? Register   Sign In
404_override doesn't work as expected..
#31

[eluser]William Rufino[/eluser]
Does anyone got a fix for this? My classses aren't loaded on the 404 page...
#32

[eluser]riggerthegeek[/eluser]
Hi all

Wouldn't normally post a link back to my blog, but I've given two solutions to this problem (easy and good). Both feel a little bit of a hack and, frankly, will remain so until CodeIgniter sorts it out properly.

Here's my thoughts - http://www.simonemms.com/code/codeigniters-404-override-problem

S
#33

[eluser]victorche[/eluser]
So many changes in Bitbucket, couple of new versions... And still no fix for this! Yeah, it works with missing controllers, but it does NOT work with existing controllers and missing methods.

Please, devs! Do something Sad
#34

[eluser]FinalFrag[/eluser]
I think I may have found a pretty clean solution.

First, create a MY_Controller.php in application/core

Add these functions to MY_Controller:

Code:
public function _remap($method, $params = array())
{
   if (method_exists($this, $method))
       {
        return call_user_func_array(array($this, $method), $params);
    }
    
    $this->show404();
}

function show404()
{
    $this->s->display('error/error');
}


Then use your favorite text replacement tool to change all calls to 'show_404();' in your application/controllers/*.php files to '$this->show404();'

I've tested this roughly and it works as expected up to now. I will do some more testing when I find the time.


Note: if you don't want to change all your 'show_404();' calls, I'm sure there is a way to overwrite the standard show_404() function. I shall look into this if someone needs some help with that.

Let me know if you find unexpected behaviour when using this method...
#35

[eluser]Glazz[/eluser]
This works, i just edited the show404, i load the view there, and i didnt needed to edit anymore files, since i was already using MY_Controller!

Thanks =)
#36

[eluser]FinalFrag[/eluser]
[quote author="Glazz" date="1305050262"]This works, i just edited the show404, i load the view there, and i didnt needed to edit anymore files, since i was already using MY_Controller!

Thanks =)[/quote]

Yes, the show404 needs a bit of change... I'm using Smarty in my version of codeigniter and I just copy pasted my code here.

Also, don't forget to add
Code:
$this->output->set_status_header('404');
to the show404 function. This is better for SEO.

Can't believe I just "fixed" an issue that has been there so long Big Grin
#37

[eluser]chriskelley[/eluser]
I ran into an output problem with the above method, outlined below with fix:

In one of my controllers I am testing for the validity of a uri segment, with the uri built in the following manner:
/class/method/id

If the "id" doesn't validate, I explicitly call $this->show404() with the intention of the controller halting at that point, as the core show_404() method does.

The problem is that $this->show404() never halts output, so it continues to load the rest of the controller. To fix, I yanked a snippet from the original show_404() method and threw it in $this->show404().

Code:
class MY_Controller extends CI_Controller {

    public function _remap($method, $params = array())
    {
        if (method_exists($this, $method)){
            return call_user_func_array(array($this, $method), $params);
        }
        
        $this->show404();
    }
    
    
    
    
    function show404()
    {
        $data['error_code'] = "404 Page Not Found";
        $data['message'] = "Sorry, we couldn't find the page you requested.";
        
        $this->output->set_status_header(404);

        if (ob_get_level() > 1)
        {
            ob_end_flush();
        }
        ob_start();
        
        $this->load->view('errors', $data);
        
        $buffer = ob_get_contents();
        ob_end_clean();
        echo $buffer;
        exit;
    }  
    
}
#38

[eluser]jacobson[/eluser]
Hello, I had the same problems as here and tried all this solutions but I have a question. The method with MY_Controller owrks properly. When I have a controller which extends the MY_Controller and try to write in the url the method which is not there it shows my custom 404 message. But all changes when it comes to showing the error when I put wrong controller http://localhost/wrongcontroller. Because "nothing extends from MY_Controller" . I hope i showed my problem clearly Tongue
thx for responding
#39

[eluser]skcin7[/eluser]
I AM STILL HAVING TROUBLE THIS IS THE MOST FRUSTRATING PROBLEM IN THE WORLD!

Sorry to yell. Smile I just upgraded from 2.0.3 to 2.1.0 and am still having this problem. I would really rather not mess around with files inside /system directory, but I will if that ends up being what I have to do.

Is anybody else still having this same problem? I've been looking into this for about a week now and can't figure out a good solution.

Is there a generally accepted solution to this problem?

[quote author="Everett Myers" date="1323931564"]
Works great from what I can see, let me know if anyone has any issues.[/quote]

Just tried altering Security Check with the code you posted and it didn't fix the problem.
#40

[eluser]Glazz[/eluser]
Sorry for bumping my own thread, but since a year has passed, and this problem has not been fixed, at least on 2.1.0, don't know if 3.0 have this fixed, din't checked the repo., just need to know if CodeIgniter DEVS will be fixing this, or not, since this is a major problem, when we want to show custom 404 error pages with dynamic contenct inside our application.




Theme © iAndrew 2016 - Forum software by © MyBB