Custom 404 control

#1
[eluser]DieterStruik[/eluser]
Hi CI-ers,

You probably ran across the same problem like me. Trying to avoid the standard 404 page which doesn't include all the CI functionality. Therefore making weird includes, cURL calls etc...

I make use of a language segment in my URI's. Like /en/home. So I needed to route like:

Code:
$route['default_controller'] = 'site';
$route['scaffolding_trigger'] = '';

// When first segment is set proceed normally (language checking done in controller)
$route['(\w{2})/(.+)'] = '$2';

// Catch all (important for 404 and language checking)
$route[':any'] = $route['default_controller'];

You don't need to change any existing functionality. Put this code below in any controller (I usually extend my main controller). It's the native _remap() method which always get called first. Here you can check stuff like a missing method.

Code:
public function _remap($method)
{
    // Check if method exists.
    if (!method_exists($this, $method))
    {
         // Show custom embedded 404 page.
         return;
    }
    // Else call the method with the arguments passed by CI.
    $args = array_slice($this->uri->segment_array(), 3);
    return call_user_func_array(array($this, $method), $args);
}

I hope this will be usefull for some of you. I haven't checked it for different types of errors. Just thought about a missing method call. Please let me know anything better.


Messages In This Thread
Custom 404 control - by El Forum - 01-15-2010, 06:28 AM
Custom 404 control - by El Forum - 01-15-2010, 07:01 AM
Custom 404 control - by El Forum - 01-15-2010, 07:08 AM
Custom 404 control - by El Forum - 01-15-2010, 08:23 AM
Custom 404 control - by El Forum - 01-15-2010, 08:25 AM
Custom 404 control - by El Forum - 01-15-2010, 08:35 AM
Custom 404 control - by El Forum - 01-15-2010, 09:11 AM
Custom 404 control - by El Forum - 05-18-2010, 08:37 AM
Custom 404 control - by El Forum - 06-16-2010, 01:08 PM
Custom 404 control - by El Forum - 06-16-2010, 02:11 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.