CodeIgniter 2.1.0设置HMVC教程

很久没有来写点什么呢,今天刚好抽空来了,工作之后你会发现时间的确好紧,一天有做不完的事,有时候回家想写点什么的,赶脚很累结果放弃了,这人参啊一个“坚持”难到一堆人,我也不例外,之前我写的大部分都是在自己的博客,但前不久自己买的那空间说是被黑了,我的娘亲,数据全没了,我也难得去纠结这件事,当时自己做博客的时候刚刚接触互联网,所以在一家小的IDC买,人家这么说我也没追究啥,幸好的是我还有备份,好了不多说了,我觉得我现在还是在这里来安家,大家都互相交流交流。今天突然觉得自己想做下翻译,献丑了,有不正确的地方,还望指出,嘿嘿。。。


CodeIgniter HMVC Tutorial – How to set up HMVC in CodeIgniter 2.1.0
CodeIgniter 2.1.0设置HMVC教程

This tutorial explains how to set up HMVC in CodeIgniter – a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications.
此教程将讲解怎么给CodeIgniter 2.1.0设置HMVC,CodeIgniter是一个轻量级的PHP框架,只需要一些简短的代码即可完成一个项目。

Key concepts about Modular Extensions – HMVC
模块扩展简介

  • HMVC stands for Hierarchical Model View Controller
    HMVC对视图、控制器和模型都是分层的
  • 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
    这些模块都是相互独立的,特别是模型、控制器和视图都是放在单独的子目录中的,所以另一个CodeIgniter项目也是可以去使用的
  • Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used to help you build view partials
    模块控制器既可以当作普通控制器使用也可以在HMVC中使用,它主要是用来生成视图模块
  • HMVC and modular separation comes into picture, when you need to add new features to an existing application without impacting the rest of the code. The code can be decoupled completely with HMVC
    HMVC and modular分层都是各自独立的,如果你想对已存在的项目增加加代码进去的话是不会影响它们的,代码完全跟HMVC分离。
  • By promoting modularity, HMVC allows systems to be more easily extended by simply adding or replacing modules
    你可以只需要增加或者代替模块即可增加模块的功能
  • To use HMVC functionality, controllers must extend the MX_Controller class
    如果要使用HMVC的函数,控制器需要继承MX_Controller类

HMVC-151x300

An example of HMVC project – MyClientBase
下面看下fusioninvoice的HMVC项目实例

A simple, intuitive, free and open source web based invoice management system developed with freelancers in mind with HMVC modular concepts. See how the project is structured into Modules, in the below screenshot
Project Url MyClientBase
一个简单很直观的免费开源的HMVC为自由职业者开发票的管理系统,根据下面的截图你便可以一览目录的结构情况。
项目地址:MyClientBase

HMVC-Example1-300x233

Installing CodeIgniter – Some Diagrams
图解CodeIgniter的安装

For beginners, here are the steps to set up CodeIgniter. If you have already installed CodeIgniter, go to SETTING UP HMVC IN CODEIGNITER 2.1
对于新手来说下面是一些安装codeigniter的简单步骤,如果你已经安装了的话,就可以跳过去看下面的CodeIgniter 2.1.0设置HMVC

Step 1.:Open the CodeIgniter home page here and browse to the Downloads page. Download the CodeIgnitor Package. I am installing version 2.1.0 downloaded on May 20 2012. You can find the complete list of available CodeIgniter installers here.
第一步:用过点击这里打开codeigniter官网首页,然后找到下载页面,把安装包下载下来,我是在2012年5月20日安装的2.1.0版本的,你可以通过这里去找最新稳定版本。
Codeigniter-Home-Page-300x91

Step 2. Unzip the package to the htdocs directory of your Web Server or extract it to a directory in your local machine and copy and paste it under htdocs.
第二步:把下载下来的文件解压到你的站点根目录去
Codeigniter-Extract

Step 3. Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at your root. htdocs is the directory that the web server software (Apache) looks for files to serve on your domain. Anything you want web-viewable needs to be inside htdocs. It applies to subdomains as well. htdocs stand for HyperText Documents, which simply mean: your internet files. I have mine under D:\xampp\htdocs
第三步:然后把CodeIgniter文件夹和文件都上传到你站点的根目录去,通常情况下index.php都会放在root目录下面的,而htdocs一般都是apache的站点目录,当然你的站点也可能是有子域名的。我的站点根目录是D:\xampp\htdocs

htdocs

Step 4. Rename the folder for convenience. For example “ci”
第四步:根据个人爱好重命名吧,比如就改为ci
Codeigniter-Rename

Step 5. The next step is to Open the application/config/config.php file with aqua data studio text editor and set your base URL.
edit-config-and-set-base-url-300x197

第五步:用编辑器打开application/config/config.php文件,按照上面的提示配置主域名即可
Step 6. If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.
第六步:下面便是设置数据库了,打开application/config/database.php文件按照上面的提示配置数据库设置即可
(ps:
$db['default']['hostname'] = ‘数据库服务地址,一般localhost’;
$db['default']['username'] = ‘数据库账户名’;
$db['default']['password'] = ‘数据库密码’;
$db['default']['database'] = ‘数据库名字’;)
codeigniter-database-settings-300x213

Try it out and test CodeIgniter Install

Point your browser to http://localhost/ci and you will get the Welcome Screen

test-codeigniter-installation1-300x227

 

SETTING UP HMVC IN CODEIGNITER 2.1

CodeIgniter 2.1.0设置HMVC

Step 1. Open codeigniter-modular-extensions-hmvc. Go to the Downloads tab and locate the zip file from here. I am installing HMVC on 2.1.0 downloaded on May 20 2012.
第一步:打开codeigniter模块扩展hmvc。找到ZIP下载地址,我是在2012年5月20日下载的2.1.0 版本。
HMVC-Download-300x184

Step 2. Unzip the package to the htdocs directory of your Web Server under the ci folder which you have made above.
第二步:把问价解压到codeigniter的目录下面,也就是上面说的ci目录下面去。
unzip-the-HMVC-package-300x73

Step 3. Move all of the files from the newly extracted folder’s ./core/ directory to CodeIgniter’s/application/core/ directory on your server.
第三步:
把刚解压的./core目录移到ci的/application/core/目录下面去。
move-to-core-300x144

Step 4. Move the MX directory and all of its files from the newly extracted folder’s ./third-party/ directory to CodeIgniter’s /application/third-party/ directory on your server.
第四步:把刚解压的./third-party/目录下的XM目录和问价一起移到ci的/application/third-party/ 目录下面去。
move-MX-directory-300x240

Step 5. Create a new folder called modules_core under applications, where you will create your HMVC modules for a project.
第五步:在项目application目录下面新建一个modules_core用来创建hmvc模块的
Step 6. The next step is to Open the application/config/config.php file with a text editor and add the following line
第六步:编辑器打开application/config/config.php文件添加如下代码
$config['modules_locations'] = array(APPPATH.’modules_core/’ => ‘../modules_core/’,);
$config['modules_locations'] = array(APPPATH.’modules_core/’ => ‘../modules_core/’,);

Download Sample CodeIgniter HMVC Files stored in github

I have kept my sample CodeIgniter – HMVC – Twitter Bootstrap framework in github for your reference. Download as a zip –> HMVC.
在github上去下载CodeIgniter HMVC示例
HTTP –> https://github.com/tariqrahiman/hmvc.git
我把我之前做个一个HMVC CodeIgniter跟Twitter相关的项目放到了github上,作为初学者的案例吧
Git只读的  –>   git://github.com/tariqrahiman/hmvc.git

SSH –> git@github.com:tariqrahiman/hmvc.git

Test the HMVC Modules using sample files
用样本文件来测试HMVC模块

To Test Modular Extension, download and extract the files from github and keep the files under your web server htdocs directory. Open a browser and browse to the location http://localhost/ci/index.php/mickey which calls a different module named hmvc using
为了测试模块扩展你把从github下载下来的问价加压到htdocs下面去,在浏览器输入:http://localhost/ci/index.php/mickey将会触发hmvc模块。
<?php echo Modules::run(‘hmvc/hmvc/index’); ?>

Online Resources
在线资源

1. CodeIgniter HMVC Setup VideoTutorial
2. CodeIgniter NewsHMVC
3. Video tutorial for CodeIgniter Beginners
4. CodeIgniter Documentation
5. Bug Tracker for CodeIgniter

原文:How to set up HMVC in CodeIgniter 2.1.0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于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、付费专栏及课程。

余额充值