Creating Custom Error Pages Codeigniter
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company codeigniter custom 404 error pages Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Custom Error Message In Codeigniter
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers,
Codeigniter Validation Custom Error Message
just like you, helping each other. Join them; it only takes a minute: Sign up Custom error pages with templates in CodeIgniter up vote 4 down vote favorite 1 I'm using the template library for CodeIgniter, http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html,
How To Set Custom Error Message In Codeigniter
and now I want to implement custom error pages too. I found one method involving a MY_Router extending the default router: http://maestric.com/doc/php/codeigniter_404 but that only treats 404 errors. I want all errors to show a simple user-friendly page, including database errors etc, and I want it to go through a controller, partly so I can use the template library, and partly so I can also implement an email function to send myself information about codeigniter custom error page the error that occurred. Someone asked about extending the functionality of the above MY_Router method for other errors, like error_db, but got no answer from the author, so I'm turning here to see if anyone knows how to do this, along the lines of the above method or any other simple way of achieving it. Please note that I'm a newbie, so do not assume too much about my knowledge of basic CodeIgniter functionality :-) php error-handling codeigniter-2 custom-error-pages share|improve this question edited Dec 15 '12 at 17:26 hakre 133k26215386 asked Oct 4 '11 at 6:55 Anders 4,1851772134 watch in you /application/errors/error_* files, there are templates db error, 404, php error and general errors, so you can work with it –user973254 Oct 19 '11 at 11:43 2 and you can extend CI core classes (exactly you need to extend CI_Exceptions) like explained here –user973254 Oct 19 '11 at 11:46 I know about the error files, but I don't know how to make them use the template library to make them look like the rest of the site. How would I extend the CI core classes to achieve this? Please provide an answer with examples... –Anders Oct 19 '11 at 23:17 I don't know how is your site being designed, but that files c
post is on creating a custom 404 page in CodeIgniter. Why you need a custom 404 error page how to set 404 page in codeigniter There are plenty of resons for having a 404 page even though codeigniter 404 override not working you are sure that you don't have any broken link on your website. Because you cannot avoid user codeigniter error 404 page not found typing incorrect URL pointing to a page on your domain which doesn't exists. Also some websites can link to some URL on your website which no longer valid. When http://stackoverflow.com/questions/7644248/custom-error-pages-with-templates-in-codeigniter there is no 404 page user is presented with the default 404 page which is set by the hosting provider which changes the impression about your website. How to setup a 404 page in CodeIgniter website? CodeIgniter provides an easy way to build a custom 404 (Page-not-found) page. Here is the step by step on setting up a http://www.thephpcode.com/blog/view/how-to-create-custom-404-page-with-codeigniter.html 404 page for your CodeIgniter website. First open routes.php in the application/config folder and set a custom controller name. $route['404_override'] = 'my404'; //my404 is class name. Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here. output->set_status_header('404'); $data['content'] = 'error_404'; // View name $this->load->view('index',$data);//loading in my template } } ?> Create a view file with name 'error_404.php' with your custom message. That is all you need to do and will be having your custom 404 page now. Best Practises for 404 pages Always send the status header as 404 for the search engines will know the pages are 404 pages and should not be indexed. Provide a search access to your website so that you can easily find what they are looking for in your website. Keep it simple and you need to always apologies for missing the page, even i
Chart Model-View-Controller Architectural Goals Tutorial Static pages News section Create news items Conclusion Contributing to CodeIgniter Writing CodeIgniter Documentation Developer's Certificate of Origin 1.1 General Topics CodeIgniter URLs https://www.codeigniter.com/user_guide/general/errors.html Controllers Reserved Names Views Models Helpers Using CodeIgniter Libraries Creating Libraries http://jeromejaglale.com/doc/php/codeigniter_404 Using CodeIgniter Drivers Creating Drivers Creating Core System Classes Creating Ancillary Classes Hooks - Extending the Framework Core Auto-loading Resources Common Functions Compatibility Functions URI Routing Error Handling Caching Profiling Your Application Running via the CLI Managing your Applications Handling Multiple Environments Alternate custom error PHP Syntax for View Files Security PHP Style Guide Libraries Benchmarking Class Caching Driver Calendaring Class Shopping Cart Class Config Class Email Class Encrypt Class Encryption Library File Uploading Class Form Validation FTP Class Image Manipulation Class Input Class Javascript Class Language Class Loader Class Migrations Class Output Class Pagination Class Template Parser Class custom error message Security Class Session Library HTML Table Class Trackback Class Typography Class Unit Testing Class URI Class User Agent Class XML-RPC and XML-RPC Server Classes Zip Encoding Class Database Reference Quick Start: Usage Examples Database Configuration Connecting to a Database Running Queries Generating Query Results Query Helper Functions Query Builder Class Transactions Getting MetaData Custom Function Calls Query Caching Database Manipulation with Database Forge Database Utilities Class Database Driver Reference Helpers Array Helper CAPTCHA Helper Cookie Helper Date Helper Directory Helper Download Helper Email Helper File Helper Form Helper HTML Helper Inflector Helper Language Helper Number Helper Path Helper Security Helper Smiley Helper String Helper Text Helper Typography Helper URL Helper XML Helper CodeIgniter Welcome to CodeIgniter Installation Instructions Downloading CodeIgniter Installation Instructions Upgrading From a Previous Version Troubleshooting CodeIgniter Overview Getting Started CodeIgniter at a Glance Supported Features Application Flow Chart Model-View-Controller Architectural Goals Tutorial Static pages News section Create news items Conclusion Contributing to CodeIgniter Writing CodeIgniter Documentation Developer's
class Error extends Controller { function error_404() { $this->output->set_status_header('404'); echo "404 - not found"; } } Add MY_Router.php system/application/libraries/MY_Router.php error_404(); function _validate_request($segments) { // Does the requested controller exist in the root folder? if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) { return $segments; } // Is the controller in a sub-folder? if (is_dir(APPPATH.'controllers/'.$segments[0])) { // Set the directory and remove it from the segment array $this->set_directory($segments[0]); $segments = array_slice($segments, 1); if (count($segments) > 0) { // Does the requested controller exist in the sub-folder? if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) { return $this->error_404(); } } else { $this->set_class($this->default_controller); $this->set_method('index'); // Does the default controller exist in the sub-folder? if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) { $this->directory = ''; return array(); } } return $segments; } // Can't find the requested controller... return $this->error_404(); } function error_404() { $this->directory = ""; $segments = array(); $segments[] = $this->error_controller; $segments[] = $this->error_method_404; return $segments; } function fetch_class() { // if method doesn't exist in class, change // class to error and method to error_404 $this->check_method(); return $this->class; } function check_method() { $ignore_remap = true; $class = $this->class; if (class_exists($class)) { // methods for this class $class_methods = array_map('strtolower', get_class_methods($class)); // ignore controllers using _remap() if($ignore_remap && in_array('_remap', $class_methods)) { return; } if (! in_array(strtolower($this->method), $class_methods)) { $this->directory = ""; $this->class = $this->erro