自定义404错误消息在codeigniter中不起作用

时间:2013-02-04 04:24:11

标签: php codeigniter

当我在控制器中访问错误的功能时,404错误页面运行良好。但是,当我访问类似“http://example.com/model/detail/116”的URL时,哪个116号错误[不在数据库中],我的404错误页面无效。

我的控制器中有这个代码:

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');
}

抱歉我的英文不好,请帮忙:-) 谢谢..

1 个答案:

答案 0 :(得分:1)

而不是:

redirect('404');

尝试使用CodeIgniter's native

show_404('page');

修改

尝试使用此代码,稍微清理一下并在保存视图之前完成检查。

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');
}