zend framework 虽然出2了,但笔者一直没机会接触,本文讲的是zend framework 1.11 的多模块配置实例,并提供一个本人亲手做的实例下载。

首先利用zend studio创建一个zend framework项目。默认的目录结构如下:

application/

---- controllers/

---- ---- IndexController.php

---- models/

---- views/

---- ---- helpers/

---- ---- scripts/

---- ---- ---- index/

---- ---- ---- ---- index.phtml

docs/

library/

public/

---- index.php


接下来进行多模块配置

1. 在application文件夹下创建一个modules文件夹

2. 在modules文件夹下再创建一个default文件夹

3. 将原先在application下的的controllers,models,views3个文件夹剪切到刚创建的default文件夹下

4.修改configs/application.ini配置文件

    在[production]最后面加上一行
    resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

    即定义多模块路径

    默认情况下,完整的application.ini大概是这样的

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
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 = 0
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

至此,默认模块就配置好了。

默认的访问路径为 http://localhost/项目名/public

然后添加一个叫admin的模块

1. 在modules文件夹下创建一个叫admin的文件夹

2. 在此文件夹下创建3个文件夹:controllers,models,views

3. 在controllers文件夹下创建一个叫IndexController.php的文件,文件内容如下


class Admin_IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        // action body
    }
}

注意admin模块的controller的类名有Admin_前缀,而default模块的controller类名不需要前缀。

而model的类名,不管是default模块还是其他模块,都需要加前缀。

所有类的文件名均无前缀。

4. 在views文件夹下创建scripts文件夹,然后在scripts文件夹下创建index文件夹,再在index文件夹下创建index.phtml文件

至此,admin模块也创建好了,访问路径为 http://localhost/项目名/public/admin


最终目录结构如下


application/

---- modules

---- ---- default

---- ---- ---- controllers/

---- ---- ---- ---- IndexController.php

---- ---- ---- models/

---- ---- ---- views/

---- ---- ---- ---- helpers/

---- ---- ---- ---- scripts/

---- ---- ---- ---- ---- index/

---- ---- ---- ---- ---- ---- index.phtml


---- ---- admin/

---- ---- ---- controllers/

---- ---- ---- ---- IndexController.php

---- ---- ---- models/

---- ---- ---- views/


---- ---- ---- ---- helpers/

---- ---- ---- ---- scripts/

---- ---- ---- ---- ---- index/

---- ---- ---- ---- ---- ---- index.phtml

docs/

library/

public/

---- index.php


实例下载 (实例中并未放置zend类库,因为那玩意儿太大了,大家可自行去别处弄个来,复制到library文件夹下即可)