{"id":14987248,"url":"https://github.com/hex-ci/codeigniter-hmvc","last_synced_at":"2025-07-01T06:35:26.885Z","repository":{"id":406478,"uuid":"25252","full_name":"hex-ci/CodeIgniter-HMVC","owner":"hex-ci","description":"Adding HMVC support to CodeIgniter. 为 CodeIgniter 增加 HMVC 支持。","archived":false,"fork":false,"pushed_at":"2017-07-01T09:06:29.000Z","size":974,"stargazers_count":138,"open_issues_count":0,"forks_count":54,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-06-02T05:55:07.676Z","etag":null,"topics":["codeigniter","hmvc","php"],"latest_commit_sha":null,"homepage":"http://codeigniter.org.cn/forums/thread-1319-1-1.html","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hex-ci.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2008-06-14T11:11:19.000Z","updated_at":"2025-01-09T14:04:33.000Z","dependencies_parsed_at":"2022-08-16T10:25:07.423Z","dependency_job_id":null,"html_url":"https://github.com/hex-ci/CodeIgniter-HMVC","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/hex-ci/CodeIgniter-HMVC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex-ci%2FCodeIgniter-HMVC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex-ci%2FCodeIgniter-HMVC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex-ci%2FCodeIgniter-HMVC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex-ci%2FCodeIgniter-HMVC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hex-ci","download_url":"https://codeload.github.com/hex-ci/CodeIgniter-HMVC/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex-ci%2FCodeIgniter-HMVC/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262914214,"owners_count":23383846,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeigniter","hmvc","php"],"created_at":"2024-09-24T14:14:19.676Z","updated_at":"2025-07-01T06:35:26.863Z","avatar_url":"https://github.com/hex-ci.png","language":"PHP","readme":"## 欢迎大家使用 CodeIgniter HMVC 扩展！\r\n\r\n\r\n如果您还不了解什么是 HMVC，请先移步维基百科查看：\r\n\r\n[http://zh.wikipedia.org/wiki/HMVC](http://zh.wikipedia.org/wiki/HMVC)\r\n\r\n\r\n一直感觉 CodeIgniter 缺乏好的 HMVC 架构，而且我个人认为目前的几个 HMVC 第三方类库都不是很好，有的要修改 CI 的源代码，有的要引入新的语法，这都不是我喜欢的方式，所以我自己思考了一个方案，希望大家多多提出宝贵意见。这个 HMVC 的特点就是不修改 CI 源代码，不引入新的语法，完全是利用 CI 强大的扩展机制。\r\n\r\n目前的扩展方式是在 application 目录下增加 modules 目录，每个模块有自己的目录，并且模块可以有一级子目录，比如 `application/modules/目录/模块名/....`\r\n\r\n每个模块都有自己的 MVC 结构，像这样 `application/modules/模块名/controllers`，`application/modules/模块名/models`，`application/modules/模块名/views` 在视图中装载模块：\r\n\r\n```php\r\n$this-\u003eload-\u003emodule('模块名/控制器/方法');\r\n```\r\n\r\n这里也可以使用 URL 路由中的默认控制器，默认的方法是 index() 方法，和普通控制器保持一致。如果要传递参数：\r\n\r\n```php\r\n$this-\u003eload-\u003emodule('模块名/控制器/方法', array('参数1', '参数2', ...));\r\n```\r\n\r\n如果需要返回模块的结果而不想输出到屏幕，可以把第 3 个参数设置为 TRUE：\r\n\r\n```php\r\n$this-\u003eload-\u003emodule('模块名', array('参数1', '参数2', ...), TRUE);\r\n```\r\n\r\n如果需要从 URL 访问某个模块的某个方法，URL 规则是这样的：\r\n\r\n```\r\nhttp://domain/index.php/module/模块名/控制器/方法\r\n```\r\n\r\n实际上 `/module` 后面的内容和前面传入 `$this-\u003eload-\u003emodule()` 中的参数一致。\r\n\r\n如果要通过 URL 传递参数，则直接加在 URL 后面：\r\n\r\n```\r\nhttp://domain/index.php/module/模块名/控制器/方法/参数1/参数2/..../参数n\r\n```\r\n\r\n另外，这里的 URI 可以使用路由规则，也就是说什么样的 URL 都可以，只要最后路由成符合上面的规则即可，比如要使用这样的 URL：\r\n\r\n```\r\nhttp://domain/index.php/m/模块名/控制器/方法\r\n```\r\n\r\n可以在 routers.php 里添加一个路由规则：\r\n\r\n```php\r\n$route['m/(:any)/(:any)/(:any)/(:any)'] = 'module/$1/$2/$3/$4';\r\n```\r\n\r\n或者\r\n\r\n```php\r\n$route['m/(.*)'] = 'module/$1';\r\n```\r\n\r\n如果要在某个模块的视图里生成访问当前模块当前控制器的某方法的 URL，可以在视图里这样写：\r\n\r\n```php\r\n\u003c?php echo $this-\u003emodule_url('要访问的方法名/参数1/..../参数n'); ?\u003e\r\n```\r\n\r\n如果要生成当前模块其他控制器的方法的 URL，可以这样：\r\n\r\n```php\r\n\u003c?php echo $this-\u003emodule_url('要访问的方法名/参数1/..../参数n', '控制器名'); ?\u003e\r\n```\r\n\r\n基本上就是这样，如果大家有不清楚的，我会在论坛详细解答：\r\n\r\n[http://codeigniter.org.cn/forums/thread-1319-1-1.html](http://codeigniter.org.cn/forums/thread-1319-1-1.html)\r\n\r\n压缩包解压后，其中有控制器、模型、视图和模块的简单例子，并且其中只包含模块所需的代码，不包含 CI 核心代码。\r\n\r\n\r\n### 更新记录\r\n\r\n-   2017.07.01 支持 CodeIgniter 3.1.5。\r\n-   2016.11.20 支持 CodeIgniter 3.1.2 \u0026 修复一些 BUG。\r\n-   2016.4.25 支持 CodeIgniter 3.0.6\r\n-   2013.4.18 修复一个在模块中的模型，无法访问当前模块变量的 BUG。\r\n-   2012.4.8 修复一个自动装载类库后，模块中此类库无法使用的 BUG。\r\n-   2012.2.19 增加对 CodeIgniter 2.1.0 的支持。\r\n-   2011.8.9 修复从 URL 访问 Module 的时候，autoload 无效的 BUG。\r\n-   2011.7.28 增加从 URL 访问 Module 的功能。\r\n-   2011.4.13 修正 autoload 对 module 无效的 BUG。\r\n-   2011.4.11 支持最新的 CI 2.0.0，完全为 PHP5 重写 HMVC 所有代码。\r\n-   2011.1.8 支持在控制器里直接载入一个或多个模块；修复在模块里装载类库报错的 BUG；\r\n-   2010.12.15 支持在控制器中直接装载模块。\r\n-   2010.8.7 修正一个在 Module 的构造函数中装载 Model 报错的 BUG。\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhex-ci%2Fcodeigniter-hmvc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhex-ci%2Fcodeigniter-hmvc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhex-ci%2Fcodeigniter-hmvc/lists"}