views:

26

answers:

1

Hi I am facing a problem with hooks in Codeigniter.I have written hook for 'post_controller' which call a Controller function and load the template (Layout view template).It works fine for all normal cases. But now i need to make a custom error page.So i am overriding show_404() function.In this function i use the get_instance(); function to get the CI object.Using the CI object i am calling a function to set the layout parameters.Now the problem i am facing is that when i try to load the view in show_404() function of my custom exception class the view is not getting loaded as the post_controller hook is not getting executed.I tried a lot of things but failed. Can anyone please suggest me any way to do this.Or any other way to render custom 404 page.

function show_404($page = ''){ // error page logic

    header("HTTP/1.1 404 Not Found");
$heading = "404 Page Not Found";
    $message = "The page you requested was not found ";
$CI =& get_instance();
$metadata[AppContants::META_TITLE] = '404 Page Not Found | Door and Window';
$metadata[AppContants::META_KEYWORDS] = '';
$metadata[AppContants::META_DESCRIPTION] = '';
$CI->setMeta($metadata);
$params['LAYOUT_MAINVIEW']='404_page';
$CI->renderView($params);// this is the function in my controller which sets the parameter for my template.Or it would also be very helpful if i can load the view directly as show below.
$CI->load->view('404_page'); // Loading view directly.

}
A: 

Take a look at this link. Essentially you'll probably want to create a controller for errors, and then subclass CI_Router to route all error requests to that controller, where you can add the hooks there or load a controller of your choice and execute hooks indirectly.

Edit: The problem could well be the response header you're sending (404) - many browsers will replace anything returned by the server by their own 404 pages. Try removing the header() call and see what happens. $CI->load->view() should work as expected. Also, if renderView() is not a global function, you may need to use a reference to the specific controller that contains the method.

mway
thanks mway for the link.I have gone through this before also.I suppose this is executed when the controller is at the route level i.e after index file is executed.My problem is that i need to call the show_404() function on many conditions which are based on the data operation i.e if data is returned or not.So i have gone for overriding show_404().Also if i use the $CI->output->get_output() function after loading a view in my show_404() function the view content are printed in tht array().the problem is that i am not able to render the view content.
sagar27
Can you post some code? `$this->load->view(...)` shouldn't be giving you a problem...
mway
i have update my question how it would be helpful.Please let me know if you need some more detail code.
sagar27