简体   繁体   English

适用于不同控制器的Codeigniter自定义404页面

[英]Codeigniter Custom 404 Page for different Controllers

I need assistance to get the correct method to display a custom 404 error page, for differect controllers. 我需要帮助以获取正确的方法来显示针对差异控制器的自定义404错误页面。 I have home, welcome as my controllers, but default controller is welcome . home, welcome作为我的控制器,但welcome默认控制器。 When the user Logs in, they are redirected to home . 用户登录后,他们将被重定向到home I need each of this conrtollers to have their own custom 404 error page. 我需要每个控制器都有自己的自定义404错误页面。

Set up a custom 404 controller in the application/config/routes.php file. 在application / config / routes.php文件中设置自定义404控制器。

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

Create a My404controller and handle all the 404 errors in the index() using $this->uri->segment(0) . 创建一个My404controller并使用$this->uri->segment(0)处理index()中的所有404错误。

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

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM