1.首先让我们设置一下我们的程序,让Zend能够自动载入方法,不需要我们手动的去载入
require_once 'Zend/Loader/Autoloader.php' //载入自动加载类<BR>$loader = Zend_Loader_Autoloader::getInstance();//自动实例化<BR>$loader->registerNamespace('Application_');//注册命名空间(只有系统默认的,和注册的才可以被自动载入)<BR>$loader->registerNamespace(array('Foo_', 'Bar_')); //多个命名空间的注册方法<BR>$loader->setFallbackAutoloader(true); //一个增加消耗的方法,不需要命名空间,直接载入所有类(不被推荐使用)<BR>
然后请注意,你的包含目录是否已经包含了,你自己的需被载入的目录
set_include_path(implode(PATH_SEPARATOR, array(<BR> realpath(APPLICATION_PATH . '/../library'),<BR> realpath(APPLICATION_PATH . '/forms/'),<BR> get_include_path(),<BR>)));<BR>//这里我们包含了我们的forms目录,方便程序的被载入<BR>
2.确认下form的目录
在application/forms/下 建立一个 Guestbook.phps
作为我们form的类文件,如下:
<?php<BR> class Application_Form_Guestbook extends Zend_Form<BR>{<BR> public function init()<BR> {<BR> // Set the method for the display form to POST<BR> $this->setMethod('post');//设置提交方式<br><br> // Add an email element<BR> $this->addElement('text', 'email', array(//原件的类型,名词,和一些其他信息的定义<BR> 'label' => 'Your email address:',<BR> 'required' => true,<BR> 'filters' => array('StringTrim'),<BR> 'validators' => array(<BR> 'EmailAddress',<BR> )<BR> ));<br><br> // Add the comment element<BR> $this->addElement('textarea', 'comment', array(<BR> 'label' => 'Please Comment:',<BR> 'required' => true,<BR> 'validators' => array(<BR> array('validator' => 'StringLength', 'options' => array(0, 20))<BR> )<BR> ));<br><br> // Add a captcha<BR> $this->addElement('captcha', 'captcha', array(<BR> 'label' => 'Please enter the 5 letters displayed below:',<BR> 'required' => true,<BR> 'captcha' => array(<BR> 'captcha' => 'Figlet',<BR> 'wordLen' => 5,<BR> 'timeout' => 300<BR> )<BR> ));<br><br> // Add the submit button<BR> $this->addElement('submit', 'submit', array(<BR> 'ignore' => true,<BR> 'label' => 'Sign Guestbook',<BR> ));<br><br> // And finally add some CSRF protection<BR> $this->addElement('hash', 'csrf', array(<BR> 'ignore' => true,<BR> ));<BR> }<BR>}<BR>
然后添加一个路由控制文件
applictaion/controller/GuestbookController.php
<?php<BR>class GuestbookController extends Zend_Controller_Action<BR>{<BR> // snipping indexAction()...<BR> public function signAction()<BR> {<BR> $request = $this->getRequest();//获取接受到得信息<BR> // include_once("../application/forms/Guestbook.php"); 手动加载类,只有不能自动载入时,才需要<BR> $form = new Application_Form_Guestbook;//实例化这个方法<br><br> if ($this->getRequest()->isPost()) {//如果是POST传递的结果<BR> if ($form->isValid($request->getPost())) {//判断传递是否有效<BR> $comment = new Application_Model_Guestbook($form->getValues());<BR> $mapper = new Application_Model_GuestbookMapper();<BR> <a style="color:transparent">来@源gao*daima.com搞@代#码网</a><strong>搞gaodaima代码</strong> $mapper->save($comment);<BR> return $this->_helper->redirector('index');<BR> }<BR> }<br><br> $this->view->form = $form;//将表单赋值给试图<BR> }<BR>}<BR>
最后添加一个简单的sign视图文件即可:
地址:application/views/scripts/guestbook/sgin.php
Please use the form below to sign our guestbook!<BR><?php<BR>$this->form->setAction($this->url());<BR>echo $this->form;<BR>