Zend Framework教程-Zend_Application_Module-Zend Framework 多模块支持


用zend studio或者zf命令创建module_demo1项目。执行如下命令,添加user,blog,picture模块。



/www/module_demo1>zf create module user
Creating 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/filters
Added a key for path module directory to the application.ini file
Updating project profile '/www/module_demo1/.zfproject.xml'




/www/module_demo1>zf create module blog
Creating 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/filters
Added a key for path module directory to the application.ini file
Updating project profile '/www/module_demo1/.zfproject.xml'




/www/module_demo1>zf create module picture
Creating 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/filters


Added a key for path module directory to the application.ini file
Updating project profile '/www/module_demo1/.zfproject.xml'




我们创建了user,blog,picture三个模块。项目结构如下:

│  .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



执行如下命令,创建为各个模块创建一个IndexController。
/www/module_demo1> zf create controller user index-action-included[=1] user
/www/module_demo1> zf create controller index index-action-included[=1] blog
/www/module_demo1> zf create controller index index-action-included[=1] picture


结构如下:


│  │      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




模块的Controller的结构如下:
以/module_demo1/application/modules/blog/controllers/IndexController.php为例
类名为模块名称_控制器名称即:Blog_IndexController。如下:
<?php
class 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文件。内容规则如下:


<?php

class  User_Model_MUser {
	
	public function getUserById() {
		return array (
				'username' => 'zhangsan',
				'id' => '1' 
		);
	}

}

格式要求如下:

模块名_Model_模型类名


调用方法如下:

<?php

class 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 = 1
phpSettings.display_errors = 1
includePaths.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 = 1

resources.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内容如下:

<?php
class User_Bootstrap extends Zend_Application_Module_Bootstrap {
	
}

5.各模块的models用来存放模型

模型的定义如下

模块名_Model_模型类名

6.控制器的定义如下:

<?php

class 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());
    }


}

模块名_控制器名称Contrller

 

7.调用模型。只需按照模型定义的类名使用即可。






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值