|
本帖最后由 xiaozhuaisnow 于 2013-5-28 11:46 编辑
最近在用hmvc开发项目,在实际项目开发中,module的controller层建立新的文件夹进行文件分类管理很有必要,现有的hmvc不支持在module的controller中建立新的文件夹。通过与hex沟通,修改了一下,在不影响原来的情况下,在controller中可建议新的目录。
修改代码如下:
1 core的my_loader
PHP复制代码
/**
* Module Loader
*
* This function lets users load and instantiate module.
*
* @access public
* @param string the module uri of the module
* @return void
*/
function module ($module_uri, $vars = array(), $return = FALSE)
{
//echo '<pre>';
//print_r($module_uri);
//print_r($vars);
if ($module_uri == '')
{
return;
}
$module_uri = trim($module_uri, '/');
$CI =& get_instance ();
$default_controller = $CI->router->default_controller;
if (strpos($module_uri, '/') === FALSE)
{
$path = '';
// 只有模块名,使用默认控制器和默认方法
$module = $module_uri;
$controller = $default_controller;
$method = 'index';
$segments = array();
}
else
{
$segments = explode('/', $module_uri);
//print_r($segments);
if (file_exists(APPPATH .'modules/'.$segments[0].'/controllers/'.$segments[1].EXT ))
{
$path = '';
$module = $segments[0];
$controller = $segments[1];
$method = isset($segments[2]) ? $segments[2] : 'index';
}
// 子目录下有模块?
elseif (is_dir(APPPATH .'modules/'.$segments[0].'/'.$segments[1].'/controllers'))
{
// Set the directory and remove it from the segment array
$path = $segments[0];
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
// 子目录下有模块?
if (is_dir(APPPATH .'modules/'.$path.'/'.$segments[0].'/controllers'))
{
$module = $segments[0];
$controller = isset($segments[1]) ? $segments[1] : $default_controller;
$method = isset($segments[2]) ? $segments[2] : 'index';
}
}
else
{
show_error ('Unable to locate the module you have specified: '.$path);
}
}
//此地方为修改支持controller下创建新文件夹代码
elseif (file_exists(APPPATH .'modules/'.$segments[0].'/controllers/'.$segments[1].'/'.$segments[2].EXT ))
{
//print_r($segments);
$path = '';
$module = $segments[0];
$controller = $segments[1].'/'.$segments[2];
$method = isset($segments[3]) ? $segments[3] : 'index';
}
else
{
show_error ('Unable to locate the module you have specified: '.$module_uri);
}
if ($path != '')
{
$path = rtrim($path, '/') . '/';
}
}
// 模块名全部小写
$module = strtolower($module);
// 必须是类似这样的模块类名:目录_模块名_控制器名_module (如:Account_Message_Home_module)
$c = str_replace(' ', '_', ucwords(str_replace('_', ' ', $controller)));
$class_name = str_replace(' ', '_', ucwords(str_replace('/', ' ', $path.$module.' '.$c))) . '_module';
// Module 的控制器文件的路径
$controller_path = APPPATH .'modules/'.$path.$module.'/controllers/'.$controller.EXT ;
if ( ! file_exists($controller_path))
{
show_error ('Unable to locate the module you have specified: '.$path.$module.'/controllers/'.$controller.EXT );
}
if ( ! class_exists('CI_Module'))
{
require_once(APPPATH .'core/Module'.EXT );
}
if (!isset($CI->$class_name))
{
// 装载 Module 控制器文件
require_once($controller_path);
// 实例化 Module 控制器
$CI->$class_name = new $class_name();
// 注意:要操作模块里的 loader 类实例
$CI->$class_name->load->_ci_module_path = $path.$module;
$CI->$class_name->load->_ci_module_class = $class_name;
$CI->$class_name->_ci_module_uri = $path.$module.'/'.$controller;
$CI->$class_name->_ci_module_method = $method;
}
$module_load =& $CI->$class_name->load;
if (strncmp($method, '_', 1) != 0 && in_array(strtolower($method), array_map('strtolower', get_class_methods($class_name))))
{
ob_start();
log_message ('debug', 'Module call: '.$class_name.'->'.$method);
// Call the requested method.
// Any URI segments present (besides the class/function) will be passed to the method for convenience
$output = call_user_func_array(array($CI->$class_name, $method), $module_load->_ci_object_to_array ($vars));
if ($return === TRUE)
{
$buffer = ob_get_contents();
@ob_end_clean();
$result = ($output) ? $output : $buffer;
return $result;
}
else
{
if (ob_get_level() > $this->_ci_ob_level + 1)
{
ob_end_flush();
}
else
{
$buffer = ob_get_contents();
$result = ($output) ? $output : $buffer;
$CI->output->append_output($result);
@ob_end_clean();
}
}
}
else
{
show_error ('Unable to locate the '.$method.' method you have specified: '.$class_name);
}
}
复制代码
2 修改third_party 中的model_proxy
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Module 代理控制器
*
* 用于转发 Module 的请求
*
* @package CodeIgniter
* @subpackage Libraries
* @author Hex
* @category HMVC
* @link http://codeigniter.org.cn/forums/thread-1319-1-2.html
*/
class Module_proxy extends CI_Controller {
function _remap ($method)
{
$total = $this->uri->total_rsegments();
if ($total < 1)
{
show_404 ('Module Not Found.');
return;
}
$param = array();
$segments = $this->uri->rsegment_array();
$uri = implode($segments, '/');
// 判断是否请求的子目录下的模块
if ($total > 1 && is_dir(APPPATH .'modules/'.$segments[1].'/'.$segments[2].'/controllers'))
{
if ($total > 4)
{
$param = array_slice($segments, 4);//print_r($param);
$uri = implode(array_slice($segments, 0, 4), '/');
}
}
//此地方为修改支持文件夹
elseif ($total >= 4 && is_dir(APPPATH .'modules/'.$segments[1].'/controllers/'.$segments[2].'/'))
{
if ($total==4){
$param = array_slice($segments, 4);
$uri = implode(array_slice($segments, 0, 4), '/');
}
if ($total>4){
$param = array_slice($segments, 4);
$uri = implode(array_slice($segments, 0, 4), '/');
}
}
else
{
if ($total > 3)
{
$param = array_slice($segments, 3);
$uri = implode(array_slice($segments, 0, 3), '/');
}
}
// 直接转发给相应的 Module 处理
$this->load->module($uri, $param);
}
function index ()
{
$this->_remap ('index');
}
}
/* End of file module_proxy.php */
/* Location: ./application/third_party/module_proxy.php */
复制代码
此为module的结构
此为well控制器代码
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ticket_Test_Well_module extends CI_Module
{
public function __construct ()
{
parent ::__construct ();
}
public function index ($aa=''){
echo 'cccaaaa';
echo $aa;
print_r($_GET);
}
}
复制代码
|
评分
-
查看全部评分
|