firewalls
firewalls

Reputation: 61

Custom 404 error message not working in codeigniter

When I access the wrong function in my controller, a 404 error page is working well. But, when i access url like 'http://example.com/model/detail/116', which 116 is wrong number [are not in the database], my 404 error page not working.

I have this code in my controller:

public function detail()
 {
    $id['id_galeri'] = $this->uri->segment(3);
    $detail = $this->app_model->getSelectedData("tbl_galeri",$id);
    foreach($detail->result() as $d)
    {
        $bc['jdl'] = "View";
        $bc['id_galeri'] = $d->id_galeri;
        $bc['nama'] = $d->nama;
        $bc['foto'] = $d->foto;
        $bc['deskripsi'] = $d->deskripsi;
        $bc['stts_input'] = "deskripsi";
    }

    if($this->uri->segment(3) == '' && $id['id_galeri'] == FALSE)
    {
        $segment_url = 0;
    }else{
        if(!is_numeric($this->uri->segment(3)) || !is_string($this->uri->segment(3))){
        redirect('404');
        }else{
        $segment_url = $this->uri->segment(3);
        }
    }

    $this->load->view('frontend/global/bg_top');
    $this->load->view('frontend/page/bg_view_model',$bc);
    $this->load->view('frontend/global/bg_footer');
}

Sorry for my bad english, please help :-) Thank you..

Upvotes: 0

Views: 206

Answers (1)

Michal M
Michal M

Reputation: 9480

Instead of:

redirect('404');

try using CodeIgniter's native:

show_404('page');

EDIT

Try this code, a bit cleaned up and the checks are done before they're saved for views use.

public function detail()
 {
    $id['id_galeri'] = $this->uri->segment(3);

    // Check if the supplied ID is numeric in the first place
    if ( ! is_numeric($id['id_galeri']))
    {
        show_404($this->uri->uri_string());
    }

    // Get the data
    $detail = $this->app_model->getSelectedData("tbl_galeri",$id);

    // Check if any records returned
    if (count($detail->result()) === 0)
    {
        show_404($this->uri->uri_string());
    }

    foreach($detail->result() as $d)
    {
        $bc['jdl'] = "View";
        $bc['id_galeri'] = $d->id_galeri;
        $bc['nama'] = $d->nama;
        $bc['foto'] = $d->foto;
        $bc['deskripsi'] = $d->deskripsi;
        $bc['stts_input'] = "deskripsi";
    }

    /**
     * Here do whatever you want with the $segment_url which doesn't seem to be
     * used in your code
     */

    $this->load->view('frontend/global/bg_top');
    $this->load->view('frontend/page/bg_view_model',$bc);
    $this->load->view('frontend/global/bg_footer');
}

Upvotes: 1

Related Questions