很久没有来写点什么呢,今天刚好抽空来了,工作之后你会发现时间的确好紧,一天有做不完的事,有时候回家想写点什么的,赶脚很累结果放弃了,这人参啊一个“坚持”难到一堆人,我也不例外,之前我写的大部分都是在自己的博客,但前不久自己买的那空间说是被黑了,我的娘亲,数据全没了,我也难得去纠结这件事,当时自己做博客的时候刚刚接触互联网,所以在一家小的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类
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
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版本的,你可以通过这里去找最新稳定版本。
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.
第二步:把下载下来的文件解压到你的站点根目录去
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
Step 4. Rename the folder for convenience. For example “ci”
第四步:根据个人爱好重命名吧,比如就改为ci
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.
第五步:用编辑器打开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'] = ‘数据库名字’;)
Try it out and test CodeIgniter Install
Point your browser to http://localhost/ci and you will get the Welcome Screen
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 版本。
Step 2. Unzip the package to the htdocs directory of your Web Server under the ci folder which you have made above.
第二步:把问价解压到codeigniter的目录下面,也就是上面说的ci目录下面去。
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/目录下面去。
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/ 目录下面去。
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