Welcome Guest, Not a member yet? Register   Sign In
Using a controller as a 404 error page?
#1

[eluser]Bluemill Media[/eluser]
Has anyone been successful in creating this behavior? (To be clear, I'd also like to retain the erroneous URL in the event that the user has made an immediately correctable typo in the URL...so, I would rather not simply perform a header redirect.)

It's common knowledge that you're less likely to lose a potential customer/subscriber/user if you can provide 'intelligent' 404 error pages. Seems it would be inconsistent to create an intelligent 404 error page with the default 'non-ignited' 404 page. So, I'd like to use a controller for the 404 page as to have access to CI libraries/models/helpers/views/etc when creating 'You might also like:' or 'Did you mean?' sections in the 404 page.

Is this easily achievable? My approach may be the problem, but in my attempts I've found that the 'get_instance()' function hasn't yet been defined, so you'd have to work all that in manually, which seems like it wouldn't be the best way to go about it.

Thanks in advance for your help. Smile
#2

[eluser]chris613[/eluser]
I am also looking for such a solution. I was very disappointed to find that there isn't full CI framework support in error_404.php. I could redirect to an actual controller (http://ellislab.com/forums/viewthread/87493/) but that means an extra browser request and a change in the url bar which I'd rather not have.
#3

[eluser]Bluemill Media[/eluser]
I'm sure there's a better solution somewhere, but here's a simple solution I've come up with by extending the CI_Router library. To change the controller your 404 drops on, change the value of the array around lines 44 and 62. A better approach might be to keep these items in a config file somewhere. The old 'show_404()' lines are still in, but commented out, as to allow n00bs like myself to make sense of things.. Tongue

Also, this extension allows for an infinite number of controller sub directories (awesome).

Code:
<?php  if( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router
{
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // ALLOWS FOR INFINITELY DEEP SUBDIRS AND USE OF A CUSTOMIZABLE 404 PAGE
    function _validate_request($segments)
    {
        if(file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }
        
        $in_dir = FALSE;
        $seg_str = '';
        for($i = 0; $i < count($segments) && $in_dir === FALSE; $i++)
        {
            $seg_str = $seg_str.$segments[$i].'/';
            if(is_dir(APPPATH.'controllers/'.$seg_str))
            {
                $dir_offset = $i + 1;
                $current_is_dir = TRUE;
                if(!isset($segments[$i + 1]) || !is_dir(APPPATH.'controllers/'.$seg_str.$segments[$i + 1].'/'))
                {
                    $in_dir = TRUE;
                }
            }
        }
        if($in_dir)
        {
            $this->set_directory(trim($seg_str, '/'));
            $segments = array_slice($segments, $dir_offset);
            
            if(count($segments) > 0)
            {
                if( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    $this->set_directory('');
                    // show_404($this->fetch_directory().$segments[0]);
                    return array('not_found');
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
                if( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }
        // show_404($segments[0]);
        return array('not_found');
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB