首先就是下载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内容如下
- <?php
- // Define path to application directory
- defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
- // Define application environment
- defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
- // Typically, you will also want to add your library/ directory
- // to the include_path, particularly if it contains your ZF installed
- set_include_path(implode(PATH_SEPARATOR, array(
- dirname(dirname(__FILE__)) . '/application/modules/ron/models/DbTable',
- dirname(dirname(__FILE__)) . '/application/modules/ron/models',
- dirname(dirname(__FILE__)) . '/application/modules/ron',
- dirname(dirname(__FILE__)) . '/application/modules',
- dirname(dirname(__FILE__)) . '/application',
- dirname(dirname(__FILE__)) . '/library',
- get_include_path(),
- )));
- /** Zend_Application */
- require_once 'Zend/Application.php';
- // Create application, bootstrap, and run
- $application = new Zend_Application(
- APPLICATION_ENV,
- APPLICATION_PATH . '/configs/application.ini'
- );
- $application->bootstrap()
- ->run();
.htaccess内容如下
- SetEnv APPLICATION_ENV development
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} -s [OR]
- RewriteCond %{REQUEST_FILENAME} -l [OR]
- RewriteCond %{REQUEST_FILENAME} -d
- RewriteRule ^.*$ - [NC,L]
- RewriteRule ^.*$ index.php [NC,L]
/application/configs/application.ini为配置文件。里面的内容为
- [production]
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
- bootstrap.class = "Bootstrap"
- ;========== 配置MVC架构
- ;autoloader.resource.basePath = APPLICATION_PATH;
- ;autoloader.resource.namespace = ""
- ;autoloader.type.model.path = models
- ;autoloader.type.model.namespace = Model
- ;autoloader.type.form.path = forms
- ;autoloader.type.form.namespace = Form
- ;========== 配置模块
- resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
- resources.frontController.moduleControllerDirectoryName = "controllers"
- resources.frontController.defaultModule = "default"
- resources.modules[] = '' ;为什么要写这个暂时不明,但是不写的话,找不到models
- ;========== 配置布局
- ;resources.layout.layoutPath = APPLICATION_PATH "/layouts"
- ;resources.layout.layout = "layout"
- ;ron.resources.layout.layoutPath = APPLICATION_PATH "/modules/ron/views/layouts"
- ;========== 数据库配置
- resources.db.adapter = PDO_MYSQL
- resources.db.params.host = "localhost"
- resources.db.params.username = "root"
- resources.db.params.password = '123456'
- resources.db.params.dbname = 'test'
- ;resources.db.params.prefix = 'oophp_'
- resources.db.isDefaultTableAdapter = TRUE
- resources.db.params.driver_options.1002 = 'SET NAMES UTF8;'
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- resources.frontController.throwExceptions = true
/library下面放zf的library目录
/application/Bootstrap.php内容为
- <?php
-
-
-
-
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
- protected function _initAppAutoload() {
- $autoloader = new Zend_Application_Module_Autoloader(array(
- 'namespace' => 'App',
- 'basePath' => dirname(__FILE__),
- ));
- return $autoloader;
- }
- }
- ?>
这样。就可以给每个模块单独配置一个Bootstrap.php文件,那样,每个模块都会到自己目录下面找models和forms
/application/modules/ron模块
controllers/IndexController.php内容为
- <?php
-
- class Ron_IndexController extends Zend_Controller_Action {
- public function indexAction() {
- $db = new Model_DbTable_Ron();
- $query = $db->getAll();
- $this->view->paginator = $query;
- $form = new Form_Post();
- if ($this->_request->isPost()) {
- $formData = $this->_request->getPost();
- $data = array(
- 'name' => $formData['name'],
- );
- $db->post($data);
- $this->_redirect('/ron');
- }
- $this->view->formMessagePost = $form;
- }
- public function loginAction() {
- echo "This is the loginAction().";
- }
- public function registerAction() {
- echo "This is the registerAction().";
- }
- }
多模块的时候,只要不是默认的模块,在类名前面都要加一个 "模块名_"。否则无法正常运行。
forms/Post.php内容为
- <?php
- class Form_Post extends Zend_Form {
- public function __construct($options = null) {
- parent::__construct($options);
- $this->setName('post_name');
- $name = new Zend_Form_Element_Text('name');
- $name->setLabel('昵称');
- $submit = new Zend_Form_Element_Submit('submit');
- $submit->setLabel('发表留言')
- ->setRequired(false)
- ->setIgnore(true);
- $this->addElements(array($name, $submit));
- $this->clearDecorators();
- $this->addDecorator('FormElements')
- ->addDecorator('HtmlTag', array('tag' => '<ul>'))
- ->addDecorator('Form');
- $this->setElementDecorators(array(
- array('ViewHelper'),
- array('Errors'),
- array('Description'),
- array('Label', array('tag'=>'<p>')),
- array('HtmlTag', array('tag' => '<li>')),
- ));
- $submit->setDecorators(array(
- array('ViewHelper'),
- array('Description'),
- array('HtmlTag', array('tag' => '<p>', 'class'=>'submit')),
- ));
- }
- }
如果放在forms/Ron目录下。则Post.php文件名不变。类名变成“Form_Ron_Post”。
models/DbTable/Ron.php内容为
- <?php
- class Model_DbTable_Ron extends Zend_Db_Table_Abstract {
- protected $_name = 'test';
- protected $_primary = 'id';
- public function getAll() {
- return $this->fetchAll();
- }
- public function post(array $data) {
- return $this->insert($data);
- }
- }
同form。如果Ron.php放在models目录下,则类名为Model_Ron
views/scripts/index/index.phtml内容为
- <table class="tablelist" width="100%">
- <tr><th>昵称</th></tr>
- <?php foreach ($this->paginator as $item): ?>
- <tr><td align="center"><?php echo $this->escape($item->name);?></td>
- </tr>
- <?php endforeach;?>
- </table>
- <?php echo $this->formMessagePost;?>
modules/ron/Bootstrap.php内容为
- <?php
-
-
-
-
- class Ron_Bootstrap extends Zend_Application_Module_Bootstrap {
- protected function _initAutoload() {
- $autoloader = new Zend_Application_Module_Autoloader(array(
- 'namespace' => '',
- 'basePath' => APPLICATION_PATH . '/modules/ron'));
- return $autoloader;
- }
- }
- ?>
modules/default目录和modules/ron目录结构是完全一模一样的。不再重复。