开发者

Forcing CodeIgniter to execute hooks functionality?

开发者 https://www.devze.com 2023-01-21 08:45 出处:网络
Hi I am facing a problem with hooks in Codeigniter.I have writtenhook for \'post_controller\' which call a Controller function and load the template (Layout view template).It works fine for all normal

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->renderV开发者_如何学Ciew($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.

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消