【发布时间】:2014-04-26 01:03:25
【问题描述】:
我已尝试使用 404_override 和 MY_Exceptions 来显示自定义错误视图。有人可以帮我吗?
这是 404_override 代码
Routes.php
$route['404_override'] = 'error/index';
错误.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Error extends CI_Controller {
public $user_id ='';
function __construct()
{
parent::__construct();
$this->load->library('login.php');
$user_Details = $this->login_library->get_user_details();
$this->user_id = $user_Details['user_id'];
}
public function index()
{
$this->output->set_status_header('404');
$this->output->set_status_header('404');
$this->load->view('view_header',$this->user_id);
$this->load->view('view_404');
$this->load->view('view_footer',$this->user_id);
}
}
这是 My_Exceptions 代码
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
$this->CI =& get_instance();
}
function show_404($page=''){
echo $heading = "404 Page Not Found";
echo $message = "The ppage you requested was not found.";
$this->config =& get_config();
$baseUrl = $this->config['base_url'];
header("location: ".$baseUrl.'error.html');
exit;
}
}
【问题讨论】:
-
不确定您要达到的目标,但这可能会有所帮助:stackoverflow.com/questions/21735851/…
-
我认为这很有解释性。这是一个简单的问题。 $route['404_override'] 有什么办法吗?
-
如果你想使用自定义的 404 错误页面,你不必扩展
CI_Exception核心类,你的Error控制器对我来说似乎很好。在index()方法中回显$this->user_id以确保它有效。 -
它不工作。我尝试了这两个东西,即 routes.php 和 my_exceptions 但我不好。我不知道我到底在哪里做错了。我应该尝试一个新的 CI 项目和 404_override 来检查它是我的其他项目的事情还是它是一个 CI 错误。
-
它在我的版本中不起作用,但在新版本中起作用。谢谢。帮助表示赞赏。我还尝试了 show_404() 任何我想调用该错误函数的地方,但它不起作用。感谢您的帮助。
标签: codeigniter exception error-handling routing http-status-code-404