I’ve been playing with Codeigniter lately, and I came to know that you can remap a function inside a Controller so that you can have dynamic pretty URL.
I just want to know can the same be done with controllers? I mean If I call http://example.com/stack, it’ll look for a controller named stack and if not found, it’ll call a fixed/remapped controller where I can take care of it.
Can this be done?
I’ve been playing with Codeigniter lately, and I came to know that you can
Share
Yes, it can be done, you achieve it by using uri routing.
In your
application/config/routes.phpyou can set your custom routes to remapping URIs.There are already 2 provided, the default one (in case no controller is called) and the 404 error routing.
Now, if you want to add custom routes, you just add them UNDER those 2 defaults, keeping in mind that they’re executed in the order they’re presented.
Say, for example, you want to remap ‘stack’ to another controller, just use:
In this way, whenever you access ‘stack’ it will be automatically mapped to ‘othercontroller’, and if that doesn’t exists..well, you get the same 404 error.
If you’re hiding index.php from the URL with .htaccess remember to insert it into the
$config['index_page'] = 'index.php';.If what you’re trying to achieve, instead, is a custom error message when a controller is not found, just override the 404 route as already suggested by @Juris Malinens, using your custom default controller to handle that situation