Codeigniter自定义404页面,适用于不同的控制器
问题描述:
I need assistance to get the correct method to display a custom 404 error page, for differect controllers. I have home, welcome
as my controllers, but default controller is welcome
. When the user Logs in, they are redirected to home
. I need each of this conrtollers to have their own custom 404 error page.
我需要帮助才能获得正确的方法来显示不同控制器的自定义404错误页面。 我有 home,welcome code>作为我的控制器,但默认控制器是
welcome code>。 当用户登录时,它们将被重定向到
home code>。 我需要每个conrtoller都有自己的自定义404错误页面。 p>
div>
答
Set up a custom 404 controller in the application/config/routes.php file.
$route['404_override'] = 'my404controller';
Create a My404controller
and handle all the 404 errors in the index()
using $this->uri->segment(0)
.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class My404controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
// $this->uri->segment(1) is *original* controller segment before routing.
set_status_header(404); // set 404 header
if ($this->uri->segment(1) == 'home') {
$this->load->view('home/home404');
return;
}
if ($this->uri->segment(1) == 'admin') {
$this->load->view('admin/admin404');
return;
}
if ($this->uri->segment(1) == 'blog') {
$this->load->view('blog/blog404');
return;
}
// If there's no match to existing controller, default to generic 404 page view.
$this->load->view('default404');
}
}