Welcome Guest, Not a member yet? Register   Sign In
Extending exceptions.. and loading libraries?
#7

[eluser]Pascal Kriete[/eluser]
I've played with this quite a bit actually (including a rather obscure method using fsockopen that I posted on here somewhere), but in the end it comes down to two realistic options:

1. Eliminate unknown controllers
2. Route everything and have a catchall route

The first one works, because the only 404 called before the super-object 'wakes up' is in the router - the check for a valid controller. The fix is an easy one - override the _validate_request function and change the bottom part:
Code:
// ...

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);  // remove this

        // Add something like this
        return array('error_controller', 'some_function');

That routes all requests for a non-existent controller to error_controller:Confusedome_function, and from there you can call show_404(). That should take care of most of the 404s you'll ever see (there is a security check in CodeIgniter.php that might cause trouble).

You're still stuck with the old-school 404 error file though.

The second option is a little more tedious, but actually a lot of fun, because it makes urls so much more flexible. And you get a real 404 controller, not just a pseudo-view thing. Example:
Code:
$route = array(
                'default_controller'                    => 'main',
                'scaffolding_trigger'                    => '',

                // Session routes
                'login'                                    => 'session/login',
                'logout'                                => 'session/logout',
                'register'                                => 'session/register',
                
                // User routes
                'home'                                    => 'main/home',
                'terms'                                    => 'main/terms',
                
                // Utility routes
                'files'                                    => 'utilities/files',
                'preferences'                            => 'utilities/prefs',
                
                // Project routes
                'project/(:num)'                        => 'project/home/$1',
                'project/create'                        => 'project/create',
                
                // Catchall 404
                '.*'                                    => 'main/error_404'
);

All that is assuming you want to maintain the url. Otherwise just toss a redirect header into your 404 template and send them to a real controller.


Messages In This Thread
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 08:47 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 09:34 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 09:47 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 09:49 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 09:51 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 10:08 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 10:31 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 11:08 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-28-2008, 11:21 PM
Extending exceptions.. and loading libraries? - by El Forum - 08-29-2008, 12:56 AM
Extending exceptions.. and loading libraries? - by El Forum - 08-29-2008, 01:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB