0

当我访问控制器中的错误功能时,404 错误页面运行良好。但是,当我访问像“ http://example.com/model/detail/116 ”这样的网址时,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');
}

对不起我的英语不好,请帮助:-)谢谢..

4

1 回答 1

1

代替:

redirect('404');

尝试使用CodeIgniter 的 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');
}
于 2013-02-04T04:28:28.850 回答