CI与HMVC

Modular Extensions - HMVC

模块化扩展可以使php的框架CI模块化,模块是一些被放在模块子目录里的独立的组件,典型的有模型,控制器,视图。它可以被移植到其他的CI应用程序中。
Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory, that can be dropped into other CodeIgniter applications.

HMVC stands for Hierarchical Model View Controller.

Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used to help you build view partials.
模块控制器可以作为正常的控制器或者层次MVC模型控制器,他们也可以被用来辅助你构建

Features:

All controllers can contain an $autoload class variable, which holds an array of items to load prior to running the constructor. This can be used together with module/config/autoload.php, however using the $autoload variable only works for that specific controller.

The Modules::$locations array may be set in the application/config.php file. ie:

<?php
    $config['modules_locations'] = array(
        APPPATH.'modules/' => '../modules/',
    );

Modules::run() output is buffered, so any data returned or output directly from the controller is caught and returned to the caller. In particular, $this->load->view() can be used as you would in a normal controller, without the need for return.

Controllers can be loaded as class variables of other controllers using $this->load->module(’module/controller’); or simply $this->load->module(’module’); if the controller name matches the module name.

Any loaded module controller can then be used like a library, ie: $this->controller->method(), but it has access to its own models and libraries independently from the caller.

All module controllers are accessible from the URL via module/controller/method or simply module/method if the module and controller names match.

If you add the _remap() method to your controllers you can prevent unwanted access to them from the URL and redirect or flag an error as you like.

Notes:

To use HMVC functionality, such as Modules::run(), controllers must extend the MX_Controller class.

To use Modular Separation only, without HMVC, controllers will extend the CodeIgniter Controller class.

You must use PHP5 style constructors in your controllers. ie:

<?php
class Xyz extends MX_Controller 
{
    function __construct()
    {
        parent::__construct();
    }
}

Constructors are not required unless you need to load or process something when the controller is first created.

All MY_ extension libraries should include (require) their equivalent MX library file and extend their equivalent MX_ class

Each module may contain a config/routes.php file where routing and a default controller can be defined for that module using: 
$route[‘module_name’] = ‘controller_name’;

Controllers may be loaded from application/controllers sub-directories.

Controllers may also be loaded from module/controllers sub-directories.

Resources may be cross loaded between modules. ie: $this->load->model(‘module/model’);

Modules::run() is now dedicated to returning view partials, and it will return buffered output (a view) from a controller. The syntax for using modules::run is a URI style segmented string and unlimited variables. ie: Modules::run(‘module/controller/method’, $param, $...);

To call a module controller from within a controller you can use $this->load->module() or Modules::load() and PHP5 method chaining is available for any object loaded by MX. ie: $this->load->library(‘validation’)->run().

To load languages for modules it is recommended to use the Loader method which will pass the active module name to the Lang instance; ie: $this->load->language('language_file');

The PHP5 spl_autoload feature allows you to freely extend your controllers, models and libraries from application/core or application/libraries base classes without the need to specifically include or require them.

The library loader has also been updated to accommodate some CI 1.7 features: ie Library aliases are accepted in the same fashion as model aliases, and loading config files from the module config directory as library parameters (re: form_validation.php) have beed added.

$config = $this->load->config(‘config_file’), Returns the loaded config array to your variable.

Models and libraries can also be loaded from sub-directories in their respective application directories.

When using Form_validation with MX you will need to assign the current controller as the $CI variable to the form_validation library to allow callback methods to function properly. (This has been discussed and resolved on the CI forums also). ie:

<?php
class Xyz extends MX_Controller 
{
    function __construct()
    {
        parent::__construct();
        
        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;
    }
}

View Partials

Using a Module as a view partial from within a view is as easy as writing:

<?php echo Modules::run('module/controller/method', $param, $...); ?>

The ‘method’ is optional, The default method is index, Parameters are optional, You may pass any number of parameters.

Modular Extensions installation

1) Start with a clean CI install.

2) Set $config[‘base_url’] correctly for your installation.

3) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

4) Drop Modular Extensions third_party files into the CI 2.0 application/third_party directory.

5) Drop Modular Extensions core files into application/core, the MY_Controller.php file is not required unless you wish to create your own Controller extension.

6) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

7) Create module directory structure application/modules/welcome/controllers.

8) Move controller application/controllers/welcome.php to application/modules/welcome/controllers/welcome.php.

9) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

10) Create directory application/modules/welcome/views.

11) Move view application/views/welcome_message.php to application/modules/welcome/views/welcome_message.php

12) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

You should now have a running Modular Extensions installation.

Installation Guide Hints:

-Steps 1-3 tell you how to get a standard CI install working - if you have a clean/tested CI install, skip to step 4.

-Steps 4-5 show that normal CI still works after installing MX - it shouldn’t interfere with the normal CI setup.

-Steps 6-8 show MX working alongside CI - controller moved to the “welcome” module, the view file remains in the CI application/views directory - MX can find module resources in several places, including the application directory.

-Steps 9-11 show MX working with both controller and view in the “welcome” module - there should be no files in the application/controllers or application/views directories.

FAQ

Q. What are modules, why should I use them?

A. http://en.wikipedia.org/wiki/Module

http://en.wikipedia.org/wiki/Modular_programming

http://blog.fedecarg.com/2008/06/28/a-modular-approach-to-web-development/

Q. What is Modular HMVC, why should I use it?

A. Modular HMVC = Multiple MVC triads

This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it.

In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.

The differences between using a library and a “Modular HMVC” HMVC class is: 1) No need to get and use the CI instance within an HMVC class 2) HMVC classes are stored in a modules directory as opposed to the libraries directory.

Q. Is Modular Extensions HMVC the same as Modular Separation?

A. Yes and No. Like Modular Separation, Modular Extensions makes modules “portable” to other installations. For example, if you make a nice self-contained model-controller-view set of files you can bring that MVC into another project by copying just one folder - everything is in one place instead of spread around model, view and controller folders.

Modular HMVC means modular MVC triads. Modular Separation and Modular Extensions allows related controllers, models, libraries, views, etc. to be grouped together in module directories and used like a mini application. But, Modular Extensions goes one step further and allows those modules to “talk” to each other. You can get controller output without having to go out through the http interface again.

关于MyQEE MyQEE是一个开源、快速、优雅的轻量级PHP框架,支持HMVC模式,建立在PHP5.2基础之上,支持多项目管理开发,数据库内置自动主从分离功能,MySQL支持事务操作功能并且支持自动嵌套功能,多驱动设计灵活适应各种环境。点击访问 [MyQEE入门指引](./manual/guide/zh-cn/starting.md)。   拒绝粗糙不堪、复杂的代码,选择MyQEE,选择为WEB艺术家创造的PHP框架吧。   MyQEE PHP框架的特色 * MyQEE是一套轻量级的框架,但不是简陋的框架,系统具备完善的底层类库和强大的扩展功能设计; * 特有的 [HMVC](./manual/guide/zh-cn/hmvc.md) (分层MVC设计)和多项目支持,开发更灵活; * 支持时下最流行的PHP包管理器 [Composer](http://getcomposer.org/) ,并且可以使用 Composer 安装 MyQEE 提供的官方类库; * 代码一致性设计:例如 `MySQL` 和 `MongoDB` 的查询语句完全不同,但是在 MyQEE 里可以做到实现90%的一致性,你的代码既可以使用在 `MySQL` 的环境里也可以用在 `MongoDB` 上; * 包括`Database`, `Cache`, `HttpClient`, `Session`, `Storage`, `Upload` 等支持多驱动,可以适应不同环境的需求,其中数据库支持 `MySQL`, `MySQLI`, `Mongo`, `SQLite`, `Postgre`,缓存支持 `Memcache`, `File`, `Redis`, `Apc`, `Database`, `SQLite`, `WinCache` 等; * 数据库提供强大的 `QueryBuilder` 功能,可实现同相同程序兼容多种数据库,解决SQL注入隐患和迁移环境后顾之忧; * 云引擎支持:支持SAE和BAE等云引擎,MyQEE网站就运行在SAE上; * 高性能和优雅的代码:经测试 MyQEE 的初始化速度比 Codeigniter 等优秀的轻量级框架还快; * 完备和详细的文档和API支持,更可简单的生成自己的团队文档; * 为团队开发而生,特别提供团队类库功能,多项目设置可以帮助团队成员之间规划独立和共用的代码; * ORM支持,提供了特有的高性能ORM; * 支持 `RESTFul`,支持 [PHPRPC](http://www.phprpc.org/); * 独创5模式运行设计:普通控制器、后台、命令行、系统调用、RESTFul的控制器相互分离,系统更加安全可靠;   安全性 * 系统内置XSS安全过滤; * 防SQL注入,强大的QueryBuilder; * 强制数据类型转换; * 普通控制器、后台、命令行、系统调用、RESTFul 5种运行模式相互隔离,安全更有保障;   MyQEE v3.0 RC2 更新日志: 本次更新在3.0RC1的基础上做了一些完善,修复了一些Bug: 完善文档生成脚本 完善模块化的拆分 数据库增加对 group_concat 的支持,MongoDB数据库驱动支持在group查询中使用distinct查询,修复MongoDB驱动力中查询slave在新的版本里可能导致连接失败的问题 增加 BigInt 类库 日期类库完善 HttpClient 增加upload方法,可实现上传文件功能 完善邮件类库,修复 Email 中上传附件bug,支持收件人姓名 邮件的格式,完善密件抄送的功能 Session 类库优化 Swift Storage 驱动完善,token验证支持v1和v2版本,优化参数传送方式,支持url方式的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值