天天看点

Zend Framework 入门

一.Create YourProject

详细请看这篇文章:

http://blog.csdn.net/u012675743/article/details/45511019

二.The BootStrap

Bootstrap用来定义你的项目资源和组件初始化。类如下:

//application/Bootstrap.php
 
class Bootstrapextends Zend_Application_Bootstrap_Bootstrap
{
}
           

详细还可以参考这篇文章:

http://blog.csdn.net/u012675743/article/details/45510903

三.Configuration

经常需要自己配置应用,默认配置文件在

application/configs/application.ini

,

其中也包含了指令用来设置PHP环境,声明bootstrap路径,

; application/configs/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"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"


[staging : production]


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


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

四.Action Controllers

一个controller应该有一个或者多个methods,这些methods可以通过浏览器被请求。通常可以写一个indexcontroller,作为站点的主页。

默认的indexcontroller为下:

// application/controllers/IndexController.php

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }
    public function indexAction()
    {
        // action body
    }
}
           

五.Views

每个controller都在application/views/scripts/下有一个对应的视图。并相应的命名为 ‘controller/controller.phtml’,主要写前台要展示的页面。

六.Create A Layout

在命令行下输入:

Zend Framework 入门

记得一定要切换到工程文件夹下,否则会出现如下提示:

Zend Framework 入门

然后打开layouts文件夹下,会出现一个scripts文件夹。

七.    Create a Model andDatabase Table

对数据库中要操作的每一个表都需要写一个表类,$_primary为表的主键,例如:

<?php
   class Book extends Zend_Db_Table{
    protected $_name = 'book';
    protected $_primary = 'id';
}
           

八.    Create A Form

使用框架的form来提交数据的入口是非常方便的。在application下创建目录forms,即application/forms,并创建相应的form class。

例如:

<?php
 
class Application_Form_Guestbook extendsZend_Form
{
 
   public function init()
    {
       // Set the method for the display form to POST
       $this->setMethod('post');
 
       // Add an email element
       $this->addElement('text', 'email', array(
           'label'      => 'Your emailaddress:',
           'required'   => true,
           'filters'    =>array('StringTrim'),
           'validators' => array(
                'EmailAddress',
           )
       ));
 
       // Add the comment element
       $this->addElement('textarea', 'comment', array(
           'label'      => 'PleaseComment:',
           'required'   => true,
           'validators' => array(
                array('validator' =>'StringLength', 'options' => array(0, 20))
                )
       ));
 
       // Add a captcha
       $this->addElement('captcha', 'captcha', array(
           'label'      => 'Please enterthe 5 letters displayed below:',
           'required'   => true,
           'captcha'    => array(
                'captcha' => 'Figlet',
               'wordLen' => 5,
                'timeout' => 300
           )
       ));

       // Add the submit button
       $this->addElement('submit', 'submit', array(
           'ignore'   => true,
           'label'    => 'Sign Guestbook',
       ));
 
       // And finally add some CSRF protection
       $this->addElement('hash', 'csrf', array(
           'ignore' => true,
       ));
    }
}