MY_Router.php 放到 system/application/libraries 目录下,就可以让 CI 的控制器支持多级子目录了。
这样,你就可以在 system/application/controllers 目录下放置更多级别的目录,访问的方式就是 index.php/目录1/目录2/目录3/控制器/方法/参数
请注意,你不需要 load,因为这个类是系统自动 load 的。

MY_Router.php 代码

 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 自定义路由类
*
* 让CI控制器支持多级目录
*
* @author SOHOCN.NET
* @copyright Copyright © 2012 - 2018 www.sohocn.net All rights reserved.
* @created 2012-12-13
* @updated 2012-12-13
* @version 1.0
*/ class MY_Router extends CI_Router
{
/**
* Set the directory name
*
* @access public
* @param string
* @return void
*/
function set_directory($dir)
{
$this->directory = $dir.'/';
} /**
* Validates the supplied segments. Attempts to determine the path to
* the controller.
*
* @access private
* @param array
* @return array
*/ function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
} // Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
{
return $segments;
} // Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
$temp = array('dir' => array(), 'path' => APPPATH.'controllers/'); foreach($segments as $k => $v)
{
$temp['path'] .= $v.'/'; if(is_dir($temp['path']))
{
$temp['dir'][] = $v;
unset($segments[$k]);
}
} $this->set_directory(implode('/', $temp['dir']));
$segments = array_values($segments);
unset($temp); if (count($segments) > 0)
{
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
{
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']); $this->set_directory('');
$this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index'); return $x;
}
else
{
show_404($this->fetch_directory().$segments[0]);
}
}
}
else
{
// Is the method being specified in the route?
if (strpos($this->default_controller, '/') !== FALSE)
{
$x = explode('/', $this->default_controller); $this->set_class($x[0]);
$this->set_method($x[1]);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
} // Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
{
$this->directory = '';
return array();
} } return $segments;
} // If we've gotten this far it means that the URI does not correlate to a valid
// controller class. We will now see if there is an override
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']); $this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index'); return $x;
} // Nothing else to do at this point but show a 404
show_404($segments[0]);
}
}
// END MY_Router Class

CodeIgniter 让控制器可以支持多级子目录的 Router 类库的更多相关文章

  1. Windows Azure Storage (22) Azure Storage如何支持多级目录

    <Windows Azure Platform 系列文章目录> 熟悉Azure平台的读者都知道,Azure Blob有三层架构.如下图:(注意blob.core.chinacloudapi ...

  2. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

  3. 自动创建WIN32下多级子目录的C++封装类

            这是 WIN32 自动创建多级子目录的 C++ 封装类,用法简单.         封装没有采用类的静态函数方式,而是在构造函数里面直接完成工作.没什么具体的原因,只是当时做成这样了, ...

  4. C#实现多级子目录Zip压缩解压实例

          参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩,类似winrar,可以选择 ...

  5. 怎样使用md命令一次建立多级子目录

    https://jingyan.baidu.com/article/37bce2be30cae21002f3a224.html 点击开始,运行,在运行窗口中输入“cmd”.   打开cmd窗口之后,用 ...

  6. windows下怎样使用md命令一次建立多级子目录

    在Windows系统中一次只能够创建一个子目录,在命令提示符窗口则可以一次性创建多个子目录,例如如果想在f盘创建多级子目录,则md 23\13\65\45,后面的数字随便都可以.如果想一次性删除多级目 ...

  7. ZIP解压缩文件的工具类【支持多级文件夹|全】

    ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...

  8. 运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】

    一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffm ...

  9. 【运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】

    一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffm ...

随机推荐

  1. .net连接access操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. Intent 隐式跳转,向下一个活动传递数据,向上一个活动返回数据。

    一.每个Intent只能指定一个action,多个Category. 使用隐式跳转,我们不仅可以跳转到自己程序内的活动,还可以启动其他程序的活动.使得Android多个程序之间的功能共享成为可能. 例 ...

  3. python操作mongodb之基础操作

    #coding:utf-8 __author__ = 'hdfs' import pymongo from pymongo import MongoClient client = MongoClien ...

  4. MariaDB 加密特性及使用方法

    版权声明:本文由吴洪辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/193 来源:腾云阁 https://www.qclo ...

  5. Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程

    原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...

  6. 两端对齐(兼容较好,支持IE)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. JS基础--问题记录

    1. {}var a={};{}是一个空的对象,是 new Object();的简写. 2.判断元素是存在 //jQuery 对象中元素的个数.当前匹配的元素个数. size 将返回相同的值. if ...

  9. App crash 报错 'NSUnknownKeyException'

    报错: *** Terminating app due to uncaught exception , reason: '[<NSObject 0x6e36ae0> setValue:fo ...

  10. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...