1. First let us set up our program, so that Zend can automatically load methods, do not require us to manually load
Copy Code code as follows:
Require_once ' zend/loader/autoloader.php '//load Auto Load Class
$loader = Zend_loader_autoloader::getinstance ()//automatic instantiation
$loader->registernamespace (' application_ ');//Register namespace (only system default, and registered can be automatically loaded)
$loader->registernamespace (Array (' Foo_ ', ' bar_ ')); Registration Methods for multiple namespaces
$loader->setfallbackautoloader (TRUE); A method of increasing consumption without namespaces, loading all classes directly (not recommended)
Then please note that your inclusion directory is already included, your own directory to be loaded
Copy Code code as follows:
Set_include_path (Implode (path_separator, Array (
Realpath (Application_path. '/.. /library '),
Realpath (Application_path. '/forms/'),
Get_include_path (),
)));
Here we include our forms directory to facilitate the loading of programs
2. Confirm the list of the form below
Build a Guestbook.phps under the application/forms/
As the class file for our form, as follows:
Copy Code code as follows:
<?php
Class Application_form_guestbook extends Zend_form
{
Public Function init ()
{
Set the method for the display form to POST
$this->setmethod (' post ');//Set Submission method
Add an email element
$this->addelement (' text ', ' email ', array (//) The type of the original, nouns, and some other information defined
' label ' => ' Your email address: ',
' Required ' => true,
' Filters ' => array (' Stringtrim '),
' validators ' => Array (
' EmailAddress ',
)
));
Add the comment element
$this->addelement (' textarea ', ' comment ', array (
' label ' => ' please Comment: ',
' Required ' => true,
' validators ' => Array (
Array (' validator ' => ' stringlength ', ' Options ' => array (0, 20))
)
));
Add a CAPTCHA
$this->addelement (' captcha ', ' Captcha ', Array (
' label ' => ' Please enter the 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,
));
}
}
Then add a Routing control file
applictaion/controller/guestbookcontroller.php
Copy Code code as follows:
<?php
Class Guestbookcontroller extends Zend_controller_action
{
Snipping Indexaction () ...
Public Function Signaction ()
{
$request = $this->getrequest ();//Get received information
Include_once (".. /application/forms/guestbook.php "); Load classes manually, only if they cannot be loaded automatically.
$form = new application_form_guestbook;//instantiation of this method
if ($this->getrequest ()->ispost ()) {//If the result of a post pass
if ($form->isvalid ($request->getpost ())) {//Determine if delivery is valid
$comment = new Application_model_guestbook ($form->getvalues ());
$mapper = new Application_model_guestbookmapper ();
$mapper->save ($comment);
return $this->_helper->redirector (' index ');
}
}
$this->view->form = $form;//Assign the form to an attempt to
}
}
Finally, add a simple sign view file:
Address: application/views/scripts/guestbook/sgin.php
Copy Code code as follows:
Please use the form below to sign our guestbook!
<?php
$this->form->setaction ($this->url ());
Echo $this->form;