用户
 找回密码
 入住 CI 中国社区
搜索
查看: 14916|回复: 33
收起左侧

[核心代码 Core] 让Codeigniter控制器支持多级目录【支持2.1.4】

[复制链接]
发表于 2013-5-28 09:59:47 | 显示全部楼层 |阅读模式
本帖最后由 longjianghu 于 2013-7-15 11:51 编辑

支持目前最新的版本2.1.3,在以前的版本基础上做了点改进。使用方法:复制上面的代码新建一PHP文件MY_Router.php,放到Application/core目录下即可。

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
 
复制代码

评分

参与人数 1威望 +5 收起 理由
Hex + 5 赞一个!

查看全部评分

 楼主| 发表于 2017-3-19 22:26:40 | 显示全部楼层
329537000 发表于 2017-3-9 09:01
3.X版本试过这个方法  function set_directory($dir) 这个函数有冲突报错 改个名后可以正常使用 谢谢分享思 ...

3.x默认已经支持多级目录
 楼主| 发表于 2014-5-13 10:28:20 | 显示全部楼层
michaelzhouh 发表于 2013-12-13 12:16
这个是不是和MY_Controller.php有冲突啊?
为什么带目录之后,不执行MY_Controller的构造函数了呢?

不会冲突,看没看你代码输出.
 楼主| 发表于 2014-5-13 10:25:56 | 显示全部楼层
mejinb 发表于 2014-5-6 11:05
楼主这种方式不支持2.1.4吗? 怎么按照您的操作试了以后还是404错误?

支持2.1.4,测试没有发现问题呢?
 楼主| 发表于 2013-5-28 13:33:40 | 显示全部楼层
本帖最后由 longjianghu 于 2013-5-28 17:35 编辑

把上面的代码保存为MY_Router.php,放到应用的core目录里。
发表于 2013-5-28 14:40:15 | 显示全部楼层
longjianghu 发表于 2013-5-28 13:33
把上面的代码保存为MY_Loader.php,放到应用的core目录里。

放到core目录里面就可以了吗?
发表于 2013-5-28 14:48:33 | 显示全部楼层
longjianghu 发表于 2013-5-28 13:33
把上面的代码保存为MY_Loader.php,放到应用的core目录里。

放到core的目录里以后该怎么引用

本人是新手

能说详细的吗
 楼主| 发表于 2013-5-28 16:14:03 | 显示全部楼层
羽毛 发表于 2013-5-28 14:48
放到core的目录里以后该怎么引用

本人是新手

放到Core目录就支持多级目录了,你在控制器里新建多级目录试一下。
发表于 2013-5-28 17:11:59 | 显示全部楼层
报错
123456.png
发表于 2013-5-28 17:13:59 | 显示全部楼层
当我把 category.php 放到 admin 的目录之后:
AAAA.png
 楼主| 发表于 2013-5-28 17:30:04 | 显示全部楼层
羽毛 发表于 2013-5-28 17:13
当我把 category.php 放到 admin 的目录之后:

你是不是改了自定义前缀?
 楼主| 发表于 2013-5-28 17:36:08 | 显示全部楼层
羽毛 发表于 2013-5-28 17:13
当我把 category.php 放到 admin 的目录之后:

不好意思我说错了文件名应该保存为MY_Router.php
发表于 2013-5-28 17:51:19 | 显示全部楼层
longjianghu 发表于 2013-5-28 17:36
不好意思我说错了文件名应该保存为MY_Router.php

改个名字就可以了

very very very very thank you

本版积分规则