Skip to content

Instantly share code, notes, and snippets.

@devyfriend
Created January 24, 2013 16:37
Show Gist options
  • Save devyfriend/4624685 to your computer and use it in GitHub Desktop.
Save devyfriend/4624685 to your computer and use it in GitHub Desktop.
custom 404 overides for codeigniter so we can just call "show_404('say something')" anywhere from the code, put this file in application/core, still need to edit $route['404_override'] = 'welcome/error_404'; in config/routes.php and create valid controller or nasty error will appear when browse to false url
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
function show_404($message = "The page you requested was not found.")
{
set_status_header(404);
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
// 1. load codeigniter to get any data (optional)
$ci =& get_instance();
// 2. set any data for template to display (optional)
$data['message'] = $message;
// 3. get the content from view and set to the master template variables (optional)
$template['partials']['navigation'] = $ci->load->view('partials/navigation', $data, true);
$template['body'] = $ci->load->view('partials/errors/error_404', $data, true);
$template['partials']['footer'] = $ci->load->view('partials/footer', $data, true);
// 4. put the master template on memory
include(APPPATH.'views/layouts/home.php');
$buffer = ob_get_contents();
ob_end_clean();
exit( $buffer );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment