Codeigniter Raise 404 Error
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 Business Learn codeigniter htaccess 404 error more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags 404 error page in codeigniter Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like codeigniter error 404 page not found you, helping each other. Join them; it only takes a minute: Sign up Redirect to show_404 in Codeigniter from a partial view up vote 2 down vote favorite I am using the HMVC pattern in CodeIgniter. I have codeigniter return 404 a controller that loads 2 modules via modules::run() function and a parameter is passed to each module. If either module cannot match the passed paramter I want to call show_404(). It works, but it loads the full HTML of the error page within my existing template so the HTML breaks and looks terrible. I think I want it to redirect to the error page so it doesn't run the 2nd module. Is there some way to
Codeigniter Throw 404
do that and not change the URL? Is it possible to just redirect to show_404() from the module without changing the URL? Here is an over simplified example of what's going on: www.example.com/users/profile/usernamehere The url calls this function in the users controller: function profile($username) { echo modules::run('user/details', $username); echo modules::run('user/friends', $username); } Which run these modules, which find out if user exists or not: function details($username) { $details = lookup_user($username); if($details == 'User Not Found') show_404(); // This seems to display within the template when what I want is for it to redirect else $this->load->view('details', $details); } function friends($username) { $details = lookup_user($username); if($friends == 'User Not Found') show_404(); // Redundant, I know, just added for this example else $this->load->view('friends', $friends); } I imagine there is just a better way to go at it, but I am not seeing it. Any ideas? codeigniter http-status-code-404 hmvc share|improve this question asked Jul 11 '12 at 0:11 Justin 10.1k65975 You could throw an exception instead of doing show_404() and catch the exception in your controller and do show_404() there. If there was no exception, just echo out both results. No need to redirect. –Zombaya Jul 11 '12 at 0:21 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote accepted You could throw an exception if there was an error in a
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
Codeigniter Show_404
About Us Learn more about Stack Overflow the company Business Learn more about show_404 codeigniter custom hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss codeigniter 404_override Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up CodeIgniter 404 page up http://stackoverflow.com/questions/11423801/redirect-to-show-404-in-codeigniter-from-a-partial-view vote 12 down vote favorite 5 When I enter a non existent url on my site it takes me to the 404 page that is specified by this in routes.php: $route['404_override'] = 'page/not_found'; So the design of the page is as I have made it in the view not_found.php in the "page" directory. However if I use this function to manually enforce a 404: show_404(); http://stackoverflow.com/questions/10775688/codeigniter-404-page It takes me to the default CodeIgniter 404 page with no style: 404 Page Not Found The page you requested was not found. How can I make it go to the same 404 page that I specified in the routes file? php codeigniter share|improve this question asked May 27 '12 at 16:55 TK123 7,8212686135 2 Possible Duplicate: stackoverflow.com/questions/8422033/… –Jeremy Harris May 27 '12 at 17:30 add a comment| 5 Answers 5 active oldest votes up vote 16 down vote All the answers here seem very outdated (as of version 2, at least) or very overkill. Here's a much simpler and manageable way, which only involves two steps: In application/config/routes.php modify the field 404_override to point to some controller: // Specify some controller of your choosing $route['404_override'] = 'MyCustom404Ctrl'; Inside the controller specified in your routes.php, implement the index() method so it loads your custom error 404 view. You might also want to set some headers so the browser knows you're returning a 404 error: // Inside application/controllers/MyCustom404Ctrl.php class MyCustom404Ctrl extends CI_Controller { public function __construct() { parent::__construct(); } public function index(){ $this->output->set_status_header('404'); // Make sure you actually have some view file nam
class Error extends Controller { function error_404() { $this->output->set_status_header('404'); echo "404 - http://jeromejaglale.com/doc/php/codeigniter_404 not found"; } } Add MY_Router.php system/application/libraries/MY_Router.php http://www.thephpcode.com/blog/view/how-to-create-custom-404-page-with-codeigniter.html ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Router extends CI_Router { var $error_controller = 'error'; var $error_method_404 = 'error_404'; function My_Router() { parent::CI_Router(); } // this is just the same 404 error method as in Router.php, with show_404() replaced by $this->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 codeigniter raise 404 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_
post is on creating a custom 404 page in CodeIgniter. Why you need a custom 404 error page There are plenty of resons for having a 404 page even though you are sure that you don't have any broken link on your website. Because you cannot avoid user 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 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 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 if use has typed some incorrect address. Appearance:It should look like the same as rest of your site. If someone appears on a page that has none of the branding or functionality that they were expecting, they are far more likely to close the window and go back to Google. Keep them engaged by keeping your branding and appearance consistent. Content:Just like any other page on your site, the tone and function of your written copy is crucial if you are going to get your visitors to tak