codeigniter 3.0 custom 404 not found error page

人盡茶涼 提交于 2019-11-30 05:20:06

问题


My problem is about custom error pages after upgrading to Codeigniter 3.0.

I used to have a Errors controller which handled 404 error page of the website - on this controller I have customized some parts of page, such as if user is logged in, it was showing username on the navigation bar and etc. (yes, on 404 error page).

After upgrading to CI 3.0, I have noticed that error pages are handled in different folder, I started to migrate to this way; but the problem is I can't load any variables to this page, such as session variables or can't even use any CI's functions on these pages.

I think this pages are supposed to be only HTML pages, but is there any way to load variables to these error pages?


回答1:


You need to set in application/config/routes.php the following route

$route['404_override'] = 'my404';

Then you need to create a new controller in your controllers directory (application/controllers/)

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

 public function index() 
 { 
    $this->output->set_status_header('404'); 
    $this->load->view('err404');//loading in custom error view
 } 
} 

And create an err404 view. Thats all!

Refer :Reserved Routes




回答2:


It's not working in codeigniter 3, I overwrite the default view (application/views/errors/html/error_404.php) and added my custom view, now show_404() is working fine.




回答3:


that is working good, but if redirect to an error, for example 404 with function show_404, your controller wont load your view, for a real 404 in the show_404 method, you have to go to the core of codeigniter and touch it.

\system\core\Common.php

this is an example of code:

function show_404(){
  $ci = get_instance();
  $ci->output->set_status_header(404);
  $ci->load->view('errors/error404_view');
  echo $ci->output->get_output();
  exit(4);
}


来源:https://stackoverflow.com/questions/34478283/codeigniter-3-0-custom-404-not-found-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!