Custom 404 handling |
[eluser]Mark van der Walle[/eluser]
After almost 2 years of using CI and posting a bit on these forums I present my first contribution to the community. It is a simple custom 404 handler that can be used to handle any path that cannot be routed to a controller. It uses an overrided Router, base controller and the _remap function. Suggestions, critics are welcome Base controller: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Router: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Errorhandler controller: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); And finally some config: Code: $config['error']['directory'] = '';
[eluser]Unknown[/eluser]
I would like to ask where you put that base controller? I have tried to use your script but it doesn't seem to work. Beside if you extend the controller, shouldn't the constructor have to be the same name of the class "MY_Controller"?
[eluser]Christian Haller[/eluser]
Hi, I have the same question. To which folder belong which file and are any settings required? It doesn't work here (1.6.3).
[eluser]lmv4321[/eluser]
I believe MY_Controller and MY_Router should be placed in the system/application/libraries folder. Note that I haven't actually tried to use any of this code.
[eluser]kRON[/eluser]
Fantastic contribution Mark! This just saved me from having to hack a way around my dynamic header and footer for error pages. I'm using the ErrorHandler class to handle all my exceptions now. Thanks again
[eluser]Randell Benavidez[/eluser]
It works! Awesome! Maybe you could document the parts of the code so others can find ways to make it simpler. Also, have you done any updates with your code?
[eluser]TheFunker[/eluser]
Works a treat. For those of you who want to know where to put things: 1. place config items in system/application/config/config.php (don't put them in a custom config file) 2. create MY_router.php, paste in code above, and place in system/application/libraries folder 3. create MY_controller.php, paste in code above, and place in system/application/libraries folder 4. create ErrorHandler.php, paste in code above, and place in system/application/controllers folder Use ErrorHandler.php to generate your custom error page; use it like any other controller (i.e. load models to query DB, load views to display custom page layout/message/etc, call other methods, etc. Bob's your Uncle. ;-) http://www.daveheslop.com
[eluser]Unknown[/eluser]
With the code as it is, the source of a resulting 404 is: Code: 404 There are 3 spaces just before the "404" -- causing troubles with XML. Any idea where that space is being generated from?
[eluser]JE[/eluser]
I am having problem in my session when I implemented your 404 library. A PHP Error was encountered Severity: Warning Message: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\petclinicsupport\system\application\libraries\MY_Router.php:79) Filename: controllers/welcome.php Line Number: 2 I included a session_start() in my welcome.php controller. How can I fix this error. Thanks
[eluser]markup2go[/eluser]
[quote author="JE" date="1252528654"]I am having problem in my session when I implemented your 404 library. A PHP Error was encountered Severity: Warning Message: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\petclinicsupport\system\application\libraries\MY_Router.php:79) Filename: controllers/welcome.php Line Number: 2 I included a session_start() in my welcome.php controller. How can I fix this error. Thanks[/quote] Don't end your PHP files with ?> or have empty lines at the end of your php files. See the PHP STYLES section of the user guide. |
Welcome Guest, Not a member yet? Register Sign In |