CodeIgniter Forums
Better 404 catching - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Better 404 catching (/showthread.php?tid=38724)

Pages: 1 2 3


Better 404 catching - El Forum - 02-16-2011

[eluser]duellsy[/eluser]
I'm not sure if there's a better way to do this, but by default the rout['404_override'] only gets triggered if the controller can't be found, it doesn't come into play if the controller exists but the method doesn't.

I've written up a little article on how to catch this, and send the user to a 404 page if the controller exists but the method doesn't:

http://www.subooa.com.au/blog/development/codeigniter/better-404-catching-with-codeigniter/


Better 404 catching - El Forum - 02-16-2011

[eluser]Basketcasesoftware[/eluser]
Good. 404s are a real pain. I wonder if the UhOh! error handler I've been using does this?


Better 404 catching - El Forum - 02-16-2011

[eluser]Glazz[/eluser]
Hi,

this doesn't work for me, i still get the default CI 404 error page.

My routes.php
Code:
$route['default_controller'] = 'inicio';
//$route['404_override'] = 'error/index';

$route['404'] = 'error/index';



// Clientes.
//
    # Paginação.
    $route['utilizadores/(:num)']                  = 'utilizadores/index/$1';

    # Ordenação.
    $route['utilizadores/[a-z]+/[a-z]+$']          = 'utilizadores/index/$1/$2';
    
    # Paginação + Ordenação.
    $route['utilizadores/[0-9]+/[a-z]+/[a-z]+$']   = 'utilizadores/index/$1/$2/$3';
    
    # Pesquisa.
    $route['utilizadores/pesquisa/[a-z]+$']        = 'utilizadores/index/$1';

    # Pesquisa + Paginação.
    $route['utilizadores/pesquisa/[a-z]+/[0-9]+$'] = 'utilizadores/index/$1/$2';

MY_Controller
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Controller extends MX_Controller
{
    function _remap($method) {
        if (in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
            $uri = $this->uri->segment_array();
            unset($uri[1]);
            unset($uri[2]);
    
            call_user_func_array(array($this, $method), $uri);
        }
        else {
            redirect('404');
        }
    }
}

I'm using HMVC, so i tried both MX_Controller and CI_Controller, but none worked, so i don't know if this is related to HMVC...


Better 404 catching - El Forum - 02-16-2011

[eluser]Basketcasesoftware[/eluser]
Have you tried it with a clean install on your development platform?

Edit: Without Modular Extensions of course Smile


Better 404 catching - El Forum - 02-17-2011

[eluser]duellsy[/eluser]
Your 404_override shouldn't be commented out, that is still needed for when the controller can't be found, this extra code allows for when the controller is found but the method doesn't exist

I use HMVC as well, and have the _remap function added directly to third_party/MX/conrtoller.php


Better 404 catching - El Forum - 02-17-2011

[eluser]Glazz[/eluser]
@Basketcasesoftware - nope i din't try it, and i wont sorry

@duellsy - don't work for me, i added the _remap function to the third_party/MX/controller.php, i have both routes in the routes.php file, don't work, don't know why :C


Better 404 catching - El Forum - 02-17-2011

[eluser]duellsy[/eluser]
What version of CI are you using, this was created for use with CI 2.0

Can you be a little more descriptive on what happens when you go to what url, rather than "don't work"


Better 404 catching - El Forum - 02-17-2011

[eluser]Glazz[/eluser]
CI 2.0 with HMVC, just shows up the default ci error page.


Better 404 catching - El Forum - 02-18-2011

[eluser]duellsy[/eluser]
For what it's worth, this is my full third_party/MX/Controller.php file contents...
Code:
class MX_Controller
{
    public $autoload = array();
    
    public function __construct()
    {
        $class = str_replace(CI::$APP->config->item('controller_suffix'), '', get_class($this));
        log_message('debug', $class." MX_Controller Initialized");    
        
        /* copy a loader instance and initialize */
        $this->load = clone load_class('Loader');
        $this->load->_init();    
        
        /* autoload module items */
        $this->load->_autoloader($this->autoload);
    }
    
    public function __get($class) {
        return CI::$APP->$class;
    }
    
    
    function _remap($method) {
        if (in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
            $uri = $this->uri->segment_array();
            unset($uri[1]);
            unset($uri[2]);
            
            call_user_func_array(array($this, $method), $uri);
        }
        else {
            redirect('404');
        }
    }
}


and the route:

Code:
$route['default_controller'] = "welcome";
$route['404_override'] = 'errors/show404';
$route['404'] = 'errors/show404';



Better 404 catching - El Forum - 02-18-2011

[eluser]Phil Sturgeon[/eluser]
Let's not confuse matters by talking about ME HMVC here, but as a quick aside $route['404'] has been removed from the latest ME.

Have you tried checking out a later development copy of Reactor? 404_override was in CodeIgniter before Reactor happened and was a little limited. We've worked on it since 2.0 to make it work in more situations.