ZF 多模块配置

首先就是下载zf,然后配置,然后新建数据库。这没啥可说的。

建一个2个字段的表。一个id,是主键自增。一个name,是varchar类型。

 

OK。正式开始。

 

建议一个目录zend

结构是这样的

zend

|----application

      |----configs

            |----application.ini

      |----modules

            |----ron

                  |----controllers

                        |----IndexController.php

                  |----forms

                        |----Post.php

 

                  |----models

                        |----DbTable

                              |----Admin.php

 

                  |----views

                        |----scripts

                              |----Index

                                    |----index.phtml

                  |----Bootstrap.php

            |----default

      |----Bootstrap.php

|----library

|----public

      |----.htaccess

      |----index.php

 

把根目录指向/public。

index.php是zf的入口文件。

 

index.php内容如下

[xhtml]  view plain copy
  1. <?php  
  2. // Define path to application directory  
  3. defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));  
  4. // Define application environment  
  5. defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));  
  6. // Typically, you will also want to add your library/ directory  
  7. // to the include_path, particularly if it contains your ZF installed  
  8. set_include_path(implode(PATH_SEPARATOR, array(  
  9.     dirname(dirname(__FILE__)) . '/application/modules/ron/models/DbTable',  
  10.     dirname(dirname(__FILE__)) . '/application/modules/ron/models',  
  11.     dirname(dirname(__FILE__)) . '/application/modules/ron',  
  12.     dirname(dirname(__FILE__)) . '/application/modules',  
  13.     dirname(dirname(__FILE__)) . '/application',  
  14.     dirname(dirname(__FILE__)) . '/library',  
  15.     get_include_path(),  
  16. )));  
  17. /** Zend_Application */  
  18. require_once 'Zend/Application.php';  
  19. // Create application, bootstrap, and run  
  20. $application = new Zend_Application(  
  21.     APPLICATION_ENV,  
  22.     APPLICATION_PATH . '/configs/application.ini'  
  23. );  
  24. $application->bootstrap()  
  25.     ->run();  

 

.htaccess内容如下

[c-sharp]  view plain copy
  1. SetEnv APPLICATION_ENV development  
  2. RewriteEngine On  
  3. RewriteCond %{REQUEST_FILENAME} -s [OR]  
  4. RewriteCond %{REQUEST_FILENAME} -l [OR]  
  5. RewriteCond %{REQUEST_FILENAME} -d  
  6. RewriteRule ^.*$ - [NC,L]  
  7. RewriteRule ^.*$ index.php [NC,L]  

 

/application/configs/application.ini为配置文件。里面的内容为

[c-sharp]  view plain copy
  1. [production]  
  2. phpSettings.display_startup_errors = 0  
  3. phpSettings.display_errors = 0  
  4. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"  
  5. bootstrap.class = "Bootstrap"  
  6. ;========== 配置MVC架构  
  7. ;autoloader.resource.basePath = APPLICATION_PATH;  
  8. ;autoloader.resource.namespace = ""  
  9. ;autoloader.type.model.path = models  
  10. ;autoloader.type.model.namespace = Model  
  11. ;autoloader.type.form.path = forms  
  12. ;autoloader.type.form.namespace = Form  
  13. ;========== 配置模块  
  14. resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"  
  15. resources.frontController.moduleControllerDirectoryName = "controllers"  
  16. resources.frontController.defaultModule = "default"  
  17. resources.modules[] = '' ;为什么要写这个暂时不明,但是不写的话,找不到models  
  18. ;========== 配置布局  
  19. ;resources.layout.layoutPath   = APPLICATION_PATH "/layouts"  
  20. ;resources.layout.layout       = "layout"  
  21. ;ron.resources.layout.layoutPath = APPLICATION_PATH "/modules/ron/views/layouts"  
  22. ;========== 数据库配置  
  23. resources.db.adapter = PDO_MYSQL  
  24. resources.db.params.host = "localhost"  
  25. resources.db.params.username = "root"  
  26. resources.db.params.password = '123456'  
  27. resources.db.params.dbname = 'test'  
  28. ;resources.db.params.prefix = 'oophp_'  
  29. resources.db.isDefaultTableAdapter = TRUE  
  30. resources.db.params.driver_options.1002 = 'SET NAMES UTF8;'  
  31. [development : production]  
  32. phpSettings.display_startup_errors = 1  
  33. phpSettings.display_errors = 1  
  34. resources.frontController.throwExceptions = true  

 

/library下面放zf的library目录

 

/application/Bootstrap.php内容为

[php]  view plain copy
  1. <?php  
  2. /*  
  3.  * To change this template, choose Tools | Templates 
  4.  * and open the template in the editor. 
  5. */  
  6. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {  
  7.     protected function _initAppAutoload() {  
  8.     $autoloader = new Zend_Application_Module_Autoloader(array(  
  9.             'namespace' => 'App',  
  10.             'basePath'  => dirname(__FILE__),  
  11.     ));  
  12.     return $autoloader;  
  13.     }  
  14. }  
  15. ?>  

这样。就可以给每个模块单独配置一个Bootstrap.php文件,那样,每个模块都会到自己目录下面找models和forms

 

/application/modules/ron模块

 

controllers/IndexController.php内容为

[php]  view plain copy
  1. <?php  
  2. /** Zend_Controller_Action */  
  3. class Ron_IndexController extends Zend_Controller_Action {  
  4.     public function indexAction() {  
  5.     $db = new Model_DbTable_Ron();  
  6.     $query = $db->getAll();  
  7.     $this->view->paginator = $query;  
  8.     $form = new Form_Post();  
  9.     if ($this->_request->isPost()) {  
  10.         $formData = $this->_request->getPost();  
  11.         $data = array(  
  12.             'name' => $formData['name'],  
  13.         );  
  14.         $db->post($data);  
  15.         $this->_redirect('/ron');  
  16.     }  
  17.     $this->view->formMessagePost = $form;  
  18.     }  
  19.     public function loginAction() {  
  20.     echo "This is the loginAction().";  
  21.     }  
  22.     public function registerAction() {  
  23.     echo "This is the registerAction().";  
  24.     }  
  25. }  

多模块的时候,只要不是默认的模块,在类名前面都要加一个   "模块名_"。否则无法正常运行。

 

forms/Post.php内容为

[php]  view plain copy
  1. <?php  
  2. class Form_Post extends Zend_Form {  
  3.     public function __construct($options = null) {  
  4.     parent::__construct($options);  
  5.     $this->setName('post_name');  
  6.     $name = new Zend_Form_Element_Text('name');  
  7.     $name->setLabel('昵称');  
  8.     $submit = new Zend_Form_Element_Submit('submit');  
  9.     $submit->setLabel('发表留言')  
  10.         ->setRequired(false)  
  11.         ->setIgnore(true);  
  12.     $this->addElements(array($name$submit));  
  13.     $this->clearDecorators();  
  14.     $this->addDecorator('FormElements')  
  15.         ->addDecorator('HtmlTag'array('tag' => '<ul>'))  
  16.         ->addDecorator('Form');  
  17.     $this->setElementDecorators(array(  
  18.         array('ViewHelper'),  
  19.         array('Errors'),  
  20.         array('Description'),  
  21.         array('Label'array('tag'=>'<p>')),  
  22.         array('HtmlTag'array('tag' => '<li>')),  
  23.     ));  
  24.     $submit->setDecorators(array(  
  25.         array('ViewHelper'),  
  26.         array('Description'),  
  27.         array('HtmlTag'array('tag' => '<p>''class'=>'submit')),  
  28.     ));  
  29.     }  
  30. }  

如果放在forms/Ron目录下。则Post.php文件名不变。类名变成“Form_Ron_Post”。

 

models/DbTable/Ron.php内容为

[php]  view plain copy
  1. <?php  
  2. class Model_DbTable_Ron extends Zend_Db_Table_Abstract {  
  3.     protected $_name = 'test';  
  4.     protected $_primary = 'id';  
  5.     public function getAll() {  
  6.     return $this->fetchAll();  
  7.     }  
  8.     public function post(array $data) {  
  9.     return $this->insert($data);  
  10.     }  
  11. }  

同form。如果Ron.php放在models目录下,则类名为Model_Ron

 

views/scripts/index/index.phtml内容为

[xhtml]  view plain copy
  1. <table class="tablelist" width="100%">  
  2. <tr><th>昵称</th></tr>  
  3. <?php foreach ($this->paginator as $item): ?>  
  4. <tr><td align="center"><?php echo $this->escape($item->name);?></td>  
  5. </tr>  
  6. <?php endforeach;?>  
  7. </table>  
  8. <?php echo $this->formMessagePost;?>  

 

modules/ron/Bootstrap.php内容为

[php]  view plain copy
  1. <?php  
  2. /*  
  3.  * To change this template, choose Tools | Templates 
  4.  * and open the template in the editor. 
  5. */  
  6. class Ron_Bootstrap extends Zend_Application_Module_Bootstrap {  
  7.     protected function _initAutoload() {  
  8.     $autoloader = new Zend_Application_Module_Autoloader(array(  
  9.             'namespace' => '',  
  10.             'basePath' => APPLICATION_PATH . '/modules/ron'));  
  11.     return $autoloader;  
  12.     }  
  13. }  
  14. ?>  

 

modules/default目录和modules/ron目录结构是完全一模一样的。不再重复。

你可能感兴趣的:(ZF 多模块配置)