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. Linux GDB Debug

    http://blog.jobbole.com/107925/ gdb 调试入门,大牛写的高质量指南 http://blog.jobbole.com/107759/ gdb是the GNU Debug ...

  2. Android布局_布局概述和LinearLayout布局

    一.布局概述: 布局为UI提供了一个可视化的结构,比如对于一个activity或者app widget的UI,你可以用两种方式声明布局: 在XML中声明UI元素 在运行时实例化布局元素(在java代码 ...

  3. 工作了3年的JAVA程序员应该具备什么技能?(zhuan)

    http://www.500d.me/article/5441.html **************************************** 来源:五百丁 作者:LZ2016-03-18 ...

  4. OpenGL的glTexCoord2f纹理坐标配置

    纹理坐标配置函数,先看定义: void glTexCoord2f (GLfloat s, GLfloat t); 1.glTexCoord2f()函数 有两个参数:GLfloat s, GLfloat ...

  5. Hbase之进行批处理操作

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  6. DI 之 3.1 DI的配置使用(肆)

    3.1.1  依赖和依赖注入 传统应用程序设计中所说的依赖一般指"类之间的关系",那先让我们复习一下类之间的关系: 泛化:表示类与类之间的继承关系.接口与接口之间的继承关系: 实现 ...

  7. call的其他应用

    看一些源码的时候常常发现例如这些的代码 Array.prototype.slice.call(arg) Object.prototype.toString.call(str) 等等 ,着一些系列的句子 ...

  8. jmeter 构建一个FTP测试计划

    添加用户 第一步你想做的每一个JMeter测试计划是添加一个 线程组 元素. 线程组告诉 JMeter的用户数量你想模拟,用户应该发送的次数 请求,他们应该发送的请求的数量. 继续添加线程组元素首先选 ...

  9. (31)odoo中的时间

    * 在model中    from datetime import datetime, timedelta    import time        from openerp.tools impor ...

  10. 用Visual C#向access添加数据

    (1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...