toiglicher
toiglicher

Reputation: 59

Redirect all 404 error pages to custom 404 page Codeigniter HMVC

I've created a custom 404 not-found method in my home controller

I've set `$route['404_override'] = 'home/notfound';

and it works well for not found url's right after the main domain (e.g. www.domain.com/uroetiuosyerit)

but if i type in a class name then a invalid url (e.g. www.domain.com/services/yersityijftnyutyum) it's showing the default codeigniter 404 content

I'm using codigniter 3 with hmvc modeler extension https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

Upvotes: 3

Views: 1412

Answers (2)

AdyDev
AdyDev

Reputation: 106

Create controller like My404 in application/controlers folder and add follow code:

<?php defined ('BASEPATH') or exit ('No direct script acces allowed');

class My404 extends CI_Controler {

  public function __construct()
  {
  parent::__construct();
  }

  public function index()
  {
  $this->output->set_status_header('404');
  $this->load->view('err404');
  }

}

Next, set up the route in the application/config/routes.php file:

$route['404_override'] = 'my404';

Now, you create view in application/view folder and add follow code:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
$ci = new CI_Controller();
$ci =& get_instance();
$ci->load->helper('url');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
   <meta charset="utf-8">
   <title>404 Page</title>
  </head>
<body>
 <div>
  <p>How to Create Custom Codeigniter 404 Error Pages </p>
   <div>
    <p><a href="<?php echo base_url(); ?>">Go Back to Home</a></p>
  </div>
 </div>
</body>
</html>

or you can customize from your theme or template.

Upvotes: 0

Vickel
Vickel

Reputation: 8007

set up a route like this for example:

$route['404_override'] = 'errormanager/my404';

then a controller errormanager with a function like:

public function my404(){

    $data['menu']=$this->Sitemanager->get_menu();

    $this->load->view('header',$data);
    $this->load->view('custom404');
    $this->load->view('footer');    
}

and a view custom404.php like this:

<div class="my404"> 404 this page does not exist</div>

As you use the HMVC approach, make sure that the controller errormanager and the view can be reached from everywhere, which means you need to locate it right in the Codeigniter controller+view folder (application\views and application\controller) and not in the extended modules HMVC structure

Upvotes: 4

Related Questions