404页面未显示在codeigniter中

时间:2016-06-23 14:15:37

标签: php codeigniter redirect http-status-code-404

我有一个页面可以通过id访问,例如http:/localhost/v2/indicator/details/directives/4

然而,当我删除id例如http:/localhost/v2/indicator/details/directiveshttp:/localhost/v2/indicator/details/directives/mail.html时,它仍会尝试访问该网页并给我错误。 我希望它能给我一个404找不到的页面。

错误

 Message: Missing argument 2 for Indicator::details()

 Filename: controllers/Indicator.php

 Line Number: 140

 Backtrace:

 File: C:\wamp\www\v2\application\controllers\Indicator.php Line: 140
 Function: _error_handler

 File: C:\wamp\www\v2\index.php Line: 315 Function: require_once
 Severity: Notice

 Message: Undefined variable: id

 Filename: controllers/Indicator.php

 Line Number: 154

 Backtrace:

 File: C:\wamp\www\v2\application\controllers\Indicator.php Line: 154
 Function: _error_handler

 File: C:\wamp\www\v2\index.php Line: 315 Function: require_once

控制器部分了解详情

 public function details($directive, $id) {

        $this->data['current_user_menu'] = '';
        if($this->ion_auth->in_group('admin'))
        {


        $data['user'] = $this->ion_auth->user()->row();
        $data['indicators']  = $this->core->getIndicators(); 
        // load views and send data
        $this->data['current_user_menu'] = $this->load->view('header', $data);
        $this->data['current_user_menu'] = $this->load->view('templates/view_indicator', $data);
        $this->data['current_user_menu'] = $this->load->view('footer_main');
       }
       else
       {
         //If no session, redirect to login page
         redirect('auth/login');
       }
    }

视图

<?php $in_id = $this->uri->segment(4);?>

自定义404控制器

    class My404 extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
    }

    public function index() 
    {
        $this->layout->set_body_attr(array('class' => 'gray-bg'));

        $this->layout->add_css_files(array('bootstrap.min.css'), base_url().'assets/css/');
        $this->layout->add_css_files(array('font-awesome.css'), base_url().'assets/font-awesome/css/');
        $this->layout->add_css_files(array('animate.css','style.css'), base_url().'assets/css/');
        $this->output->set_status_header('404');
        $this->load->view('404');//loading in my template 
    } 
}

1 个答案:

答案 0 :(得分:2)

试试这个

<强>功能

  1. is_int in php.net
  2. empty in php.net
  3. show_404 in codeigniter.com
  4. 在指标控制器

    public function details($directive, $id) {
    
        if (!is_int($id) || empty($id)) 
        {
            show_404();
        } 
        else {
            $this->data['current_user_menu'] = '';
            if($this->ion_auth->in_group('admin'))
            {
                $data['user'] = $this->ion_auth->user()->row();
                $data['indicators']  = $this->core->getIndicators(); 
                // load views and send data
                $this->data['current_user_menu'] = $this->load->view('header', $data);
                $this->data['current_user_menu'] = $this->load->view('templates/view_indicator', $data);
                $this->data['current_user_menu'] = $this->load->view('footer_main');
            }
            else
            {
                //If no session, redirect to login page
                redirect('auth/login');
            }
        }
    }