Welcome Guest, Not a member yet? Register   Sign In
show_404() doesn't use my custom 404 page
#1

Hello all,

On my CodeIgniter site, I set up a custom 404 page using the following code.

config/routes.php
PHP Code:
$route['404_override'] = 'error_pages/error_404_custom'

controllers/Error_pages.php
PHP Code:
<?php 
class Error_pages extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('race_model');
    }
    
    public 
function error_404_custom()
    {
        $this->output->set_status_header(404);
        
        load_page_with_great_races_sidebar
($this'errors/html/error_404_custom');
    }


helpers/mv_helper.php (autoloaded)

PHP Code:
    if ( ! function_exists('load_page_with_great_races_sidebar') )
    {
        function load_page_with_great_races_sidebar($obj$page_name$data NULL)
        {
            $data['great_races'] = html_escape($obj->race_model->get_races());
            $obj->load->view('templates/header');
            $obj->load->view('templates/great_races_header');
            $obj->load->view($page_name$data);
            $obj->load->view('templates/great_races_footer'$data);
            $obj->load->view('templates/footer');
        }
    

Works like a charm in most scenarios. Except one. If I try to use the show_404() function in a controller to throw a 404 error manually, I get the generic 404 page (which I left in the views/errors/html folder and didn't delete).

What should I change so that my custom 404 page gets loaded by the show_404() function? Or should I use a different function?

(I don't think I can simply replace views/errors/html/error_404.php with my custom error page, because I also do a database query and load the header and footer)


Thanks.
Reply
#2

(This post was last modified: 09-06-2016, 06:28 AM by Tecvid.)

this is imposible until u will override the show_404() function, from the user_guide:

You are encouraged to always have a default route as otherwise a 404 page will appear by default.
PHP Code:
$route['404_override'] = ''
This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. Same per-directory rules as with ‘default_controller’ apply here as well.

It won’t affect to the show_404() function, which will continue loading the default error_404.php file at application/views/errors/error_404.php.
Reply
#3

Any other ideas?

I might end up making my own show_my_404() function and ending it with exit(4);
Reply
#4

(This post was last modified: 09-06-2016, 10:41 PM by wolfgang1983.)

If your using latest version of CI3 will not work in sub folders


Code:
$route['404_override'] = 'error_pages/error_404_custom';

Try instead controllers > Error_pages.php

Code:
$route['404_override'] = 'error_pages'; // This will be a controller name

http://www.codeigniter.com/user_guide/ge...ile-naming
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB