codeigniter 3.0实现hmvc扩展的方法(待测试)
Codeigniter 3.0 HMVC扩展项目地址:https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
使用方法:
1、安装HMVC扩展,在core、third_party目录中添加项目文件
2、建立 modules 目录,在该目录中建立独立的controllers、models、views目录
3、在modules中的controllers必须继承MX_Controller类
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MX_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
}
其中constructor方法不是必须的,可以根据需要自行设置。
4、module的使用
<?php
/** module and controller names are different, you must include the method name also, including 'index' **/
modules::run('module/controller/method', $params, $...);
/** module and controller names are the same but the method is not 'index' **/
modules::run('module/method', $params, $...);
/** module and controller names are the same and the method is 'index' **/
modules::run('module', $params, $...);
/** Parameters are optional, You may pass any number of parameters. **/
示例1:在controller中调用
public function index()
{
echo modules::run('welcome2/welcome2/index', 'param1','param2');
$this->load->view('welcome_message');
}
除了在controller中调用module外,我们还可以在model、view中调用。
当module调用时带有参数,我们该怎么书写相应function呢?
public function index($param1, $param2)
{
$this->load->view('welcome_message');
}
可以看到,书写参数的方式和正常function一样。
说明:使用该HMVC扩展后,后续所有的MVC都需要写在modules中。module中使用library、model方式不变。
转自:http://www.sumiaowen.com/category/web-back-end/18.html
最后由 Leo 编辑于2015年05月23日 08:56