drb0901500211 2017-03-05 15:42
浏览 198
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dongtang8678 2017-03-05 19:17
    关注

    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');
    
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路