【问题标题】:404 Page not showing in codeigniter404页面未在codeigniter中显示
【发布时间】:2016-10-25 22:36:11
【问题描述】:

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

但是,当我删除 id 例如 http:/localhost/v2/indicator/details/directiveshttp:/localhost/v2/indicator/details/directives/mail.html 时,它仍然会尝试访问该页面并给我错误。 我希望它给我一个 404 not found 页面。

错误

 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 
    } 
}

【问题讨论】:

  • 我们不能梦想你的代码,你应该发布你的`Indicator::details()`控制器的代码。
  • @ArshSingh 我已经做了一些编辑,如果有帮助的话我不会这样做

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


【解决方案1】:

这样试试

函数

  1. is_int in php.net
  2. empty in php.net
  3. show_404 in codeigniter.com

在指标控制器中

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

【讨论】:

  • 我将$id 参数设为可选,因为这似乎是问题的症结所在。 public function details($directive, $id = null)
  • 我的 404 是自定义的,并且有一个控制器……我该怎么称呼它。 查看编辑
  • 像这样使用 redirect `redirect('auth/login');`
  • @Abdulla 重定向适用于http:/localhost/v2/indicator/details/directives,但不适用于http:/localhost/v2/indicator/details/directives/mail.html
  • @DanielOmara 抱歉。更改此if (!is_int($id) || empty($id))
猜你喜欢
  • 2017-07-02
  • 2012-07-12
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 2011-02-08
  • 2023-03-09
  • 2015-07-28
相关资源
最近更新 更多