用zend studio或者zf命令创建module_demo1项目。执行如下命令,添加user,blog,picture模块。
/www/module_demo1>zf create module userCreating the following module and artifacts:/www/module_demo1/application/modules/user/controllers/www/module_demo1/application/modules/user/models/www/module_demo1/application/modules/user/views/www/module_demo1/application/modules/user/views/scripts/www/module_demo1/application/modules/user/views/helpers/www/module_demo1/application/modules/user/views/filtersAdded a key for path module directory to the application.ini fileUpdating project profile '/www/module_demo1/.zfproject.xml'/www/module_demo1>zf create module blogCreating the following module and artifacts:/www/module_demo1/application/modules/blog/controllers/www/module_demo1/application/modules/blog/models/www/module_demo1/application/modules/blog/views/www/module_demo1/application/modules/blog/views/scripts/www/module_demo1/application/modules/blog/views/helpers/www/module_demo1/application/modules/blog/views/filtersAdded a key for path module directory to the application.ini fileUpdating project profile '/www/module_demo1/.zfproject.xml'/www/module_demo1>zf create module pictureCreating the following module and artifacts:/www/module_demo1/application/modules/picture/controllers/www/module_demo1/application/modules/picture/models/www/module_demo1/application/modules/picture/views/www/module_demo1/application/modules/picture/views/scripts/www/module_demo1/application/modules/picture/views/helpers/www/module_demo1/application/modules/picture/views/filtersAdded a key for path module directory to the application.ini fileUpdating project profile '/www/module_demo1/.zfproject.xml'
│ .buildpath│ .project│ .zfproject.xml│├─.settings│ .jsdtscope│ org.eclipse.php.core.prefs│ org.eclipse.wst.jsdt.ui.superType.container│ org.eclipse.wst.jsdt.ui.superType.name│├─application│ │ Bootstrap.php│ ││ ├─configs│ │ application.ini│ ││ ├─controllers│ │ ErrorController.php│ │ IndexController.php│ ││ ├─models│ ├─modules│ │ ├─blog│ │ │ ├─controllers│ │ │ ├─models│ │ │ └─views│ │ │ ├─filters│ │ │ ├─helpers│ │ │ └─scripts│ │ ├─picture│ │ │ ├─controllers│ │ │ ├─models│ │ │ └─views│ │ │ ├─filters│ │ │ ├─helpers│ │ │ └─scripts│ │ └─user│ │ ├─controllers│ │ ├─models│ │ └─views│ │ ├─filters│ │ ├─helpers│ │ └─scripts│ └─views│ ├─helpers│ └─scripts│ ├─error│ │ error.phtml│ ││ └─index│ index.phtml│├─docs│ README.txt│├─library├─public│ .htaccess│ index.php│└─tests │ bootstrap.php │ phpunit.xml │ ├─application │ └─controllers │ IndexControllerTest.php │ └─library
│ │ application.ini│ ││ ├─controllers│ │ ErrorController.php│ │ IndexController.php│ ││ ├─models│ ├─modules│ │ ├─blog│ │ │ ├─controllers│ │ │ │ IndexController.php│ │ │ ││ │ │ ├─models│ │ │ └─views│ │ │ ├─filters│ │ │ ├─helpers│ │ │ └─scripts│ │ │ └─index│ │ │ index.phtml│ │ ││ │ ├─picture│ │ │ ├─controllers│ │ │ │ IndexController.php│ │ │ ││ │ │ ├─models│ │ │ └─views│ │ │ ├─filters│ │ │ ├─helpers│ │ │ └─scripts│ │ │ └─index│ │ │ index.phtml│ │ ││ │ └─user│ │ ├─controllers│ │ │ UserController.php│ │ ││ │ ├─models│ │ └─views│ │ ├─filters│ │ ├─helpers│ │ └─scripts│ │ └─index│ │ index.phtml│ ││ └─views│ ├─helpers│ └─scripts│ ├─error│ │ error.phtml│ ││ └─index│ index.phtml│├─docs
<?phpclass Blog_IndexController extends Zend_Controller_Action{ public function init() { /* Initialize action controller here */ } public function indexAction() { // action body }}
这样,一个简单的模块就创建完成了。
可以通过访问
http://www.localzend.com/module_demo1/public/blog/index/index
来访问blog模块的index控制器的index Action。
多模块的Model支持
以user模块为例。
│ │ ││ │ └─user│ │ │ Bootstrap.php│ │ ││ │ ├─controllers│ │ │ IndexController.php│ │ ││ │ ├─models│ │ │ MUser.php│ │ ││ │ └─views│ │ ├─filters│ │ ├─helpers│ │ └─scripts│ │ └─index│ │ index.phtml
/module_demo1/application/modules/user/models/MUser.php
在user模块的models新建一个MUser.php文件。内容规则如下:
<?phpclass User_Model_MUser { public function getUserById() { return array ( 'username' => 'zhangsan', 'id' => '1' ); }}
模块名_Model_模型类名
调用方法如下:
<?phpclass User_IndexController extends Zend_Controller_Action{ private $_user = null; public function init() { $this->_user = new User_Model_MUser(); /* Initialize action controller here */ } public function indexAction() { $this->view->assign('user',$this->_user->getUserById()); }}
/module_demo1/application/modules/user/views/scripts/index/index.phtml
<br /><br /><div id="view-content"> <p>View script for controller <b>Index</b> and script/action name <b>index</b></p> <p> <?php var_dump($this->user); ?> </p></div>
总结:实现zend framework支持多模块的方法如下:
1.配置文件
/module_demo1/application/configs/application.ini
[production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 1resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"resources.modules[] =[staging : production]
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"resources.modules[] =
2.各模块放在配置文件中指定的模块存放目录。默认是/module_demo1/application/modules
3.各模块的基本结构如下:
│ │ │
│ │ └─user
│ │ │ Bootstrap.php
│ │ │
│ │ ├─controllers
│ │ │ IndexController.php
│ │ │
│ │ ├─models
│ │ │ MUser.php
│ │ │
│ │ └─views
│ │ ├─filters
│ │ ├─helpers
│ │ └─scripts
│ │ └─index
│ │ index.phtml
4.模块的Bootstrap.php内容如下:
<?phpclass User_Bootstrap extends Zend_Application_Module_Bootstrap { }
模型的定义如下
模块名_Model_模型类名
6.控制器的定义如下:
<?phpclass User_IndexController extends Zend_Controller_Action{ private $_user = null; public function init() { $this->_user = new User_Model_MUser(); /* Initialize action controller here */ } public function indexAction() { $this->view->assign('user',$this->_user->getUserById()); }}
7.调用模型。只需按照模型定义的类名使用即可。
上一篇 成功的网站需要有一个清晰的目标