• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
show_404 and 404_override

#1
[eluser]William Rufino[/eluser]
Hello everyone,

With the new 404_override, we can set a controller do handle 404 errors, etc.

What if I need to invoke this 404, with the show_404, if I try that, the show_404 function just shows the dafault error page.

#2
[eluser]William Rufino[/eluser]
No one to help ?

#3
[eluser]WanWizard[/eluser]
show_404() doesn't use routing, it directly called the 404 handler in the Exceptions class.

You could try fetching it yourself
Code:
function my_404()
{
    include APPPATH.'config/routes.php';

    if ( ! empty($route['404_override']))
    {
        redirect($route['404_override']);
    }
    else
    {
        show_404();
    }
}
Note that this requires the 404 controller to be routable...

#4
[eluser]William Rufino[/eluser]
There's only one problem with that solution, I wanted to keep the url :/
...

How can i call a method from a controller within antoher controller? i'm gonna try to handle my 404 myself oO but that would need another controller right?

#5
[eluser]WanWizard[/eluser]
In standard CI, you can't.

There are several options available (in order of my preference):
- use a base controller to extend your controllers of, and do your global thing in that base controller (like load a 404 view)
- use a modular solution like Modular Extensions HMVC or Modular CI that solve this problem
- use a model/library to deal with your 404 (breaks MVC principles, but works without problems. be careful with output buffering when you exit)

or the dirty trick I used once:
- use the post_controller_constructor hook to check a switch (p.e. a session variable), then do
Code:
include APPPATH.'controllers/404controller.php';
$GLOBALS['CI'] = new 404controller();
$GLOBALS['class'] = '404controller';
$GLOBALS['method'] = 'show_404';
to tell the bootstrap to call your controller method instead of the one routed to when returning from the hook...

p.s. from a searchengine / SEO point of view, I would advise you to not hide the 404, but redirect to a 404 controller that displays a proper 404 page with the correct message, and instructions for your user. You don't want the googles of the world to block your site because valid url's sometimes return 404's instead of 200's...

#6
[eluser]Unknown[/eluser]
Just had this same problem, and you can get around it by extending the Exceptions Class, something like this:

Code:
<?php
// application/core/MY_Exceptions.php
class MY_Exceptions extends CI_Exceptions {

    public function show_404($page = '', $log_error = TRUE)
    {
     include APPPATH . 'config/routes.php';

     // By default we log this, but allow a dev to skip it
  if ($log_error)
  {
   log_message('error', '404 Page Not Found --> '.$page);
  }

  if(!empty($route['404_override']) ){
   $CI =& get_instance();

   $CI->load->view('my_view');
         echo $CI->output->get_output();
         exit;
  } else {
  
   $heading = "404 Page Not Found";
   $message = "The page you requested was not found.";

  

   echo $this->show_error($heading, $message, 'error_404', 404);
   exit;
  }
    }

}

Hope it helps

#7
[eluser]taber[/eluser]
Cool, that worked!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.