Custom 404 Without file_get_contents or Curl - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Custom 404 Without file_get_contents or Curl (/showthread.php?tid=31470) |
Custom 404 Without file_get_contents or Curl - El Forum - 06-20-2010 [eluser]Unknown[/eluser] Lately I've needed my 404 pages to function as controllers and I wanted to avoid using file_get_contents or curl, so I've done the following to serve a custom 404 controller if a requested controller of function is not found. 1) Create a new router library to overwrite the _validate_request functionality 2) Add a line in the config file to define a 404 controller Step 1 ----------------------------- First, create the file Code: ./system/application/libraries/MY_Router.php Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Step 2 ----------------------------- Then, open your Code: ./system/application/config/config.php Code: /* Description ----------------------------- Essentially what this does is intercepts the call to the default 404 function in CodeIgniter and checks to see if you've defined a 404 controller. If the 404 controller has been defined, it runs the path specified in the config file through the request validator to verify that the controller actually exists. If the controller does exist, it is served. If it does not exist, the default 404 function is invoked instead. Notes ----------------------------- - If you've changed the class extension prefix in your config.php file, you'll need to name the file I've called MY_Router.php to match your selected prefix. - This does not modify the behavior of the show_404 function. You'll have to get creative in your controllers when requested data is not returned. I haven't found it to be too troublesome not using show_404 as I often try to create 404 pages that are relevant the the area of the site the user is having trouble in (articles, news, registration, etc.), where a static or generic 404 wouldn't be extremely useful anyway. |