Home Backend Development PHP Tutorial 解析在zend Farmework下如何创立一个FORM表单_php技巧

解析在zend Farmework下如何创立一个FORM表单_php技巧

May 17, 2016 am 08:58 AM
zend

1.首先让我们设置一下我们的程序,让Zend能够自动载入方法,不需要我们手动的去载入
复制代码 代码如下:

require_once 'Zend/Loader/Autoloader.php'    //载入自动加载类
$loader = Zend_Loader_Autoloader::getInstance();//自动实例化
$loader->registerNamespace('Application_');//注册命名空间(只有系统默认的,和注册的才可以被自动载入)
$loader->registerNamespace(array('Foo_', 'Bar_')); //多个命名空间的注册方法
$loader->setFallbackAutoloader(true);  //一个增加消耗的方法,不需要命名空间,直接载入所有类(不被推荐使用)

然后请注意,你的包含目录是否已经包含了,你自己的需被载入的目录
复制代码 代码如下:

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/forms/'),
    get_include_path(),
)));
//这里我们包含了我们的forms目录,方便程序的被载入

2.确认下form的目录
在application/forms/下 建立一个  Guestbook.phps
作为我们form的类文件,如下:
复制代码 代码如下:

 class Application_Form_Guestbook extends Zend_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 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,
        ));
    }
}

然后添加一个路由控制文件
applictaion/controller/GuestbookController.php
复制代码 代码如下:

class GuestbookController extends Zend_Controller_Action
{
    // snipping indexAction()...
    public function signAction()
    {
        $request = $this->getRequest();//获取接受到得信息
       // include_once("../application/forms/Guestbook.php");  手动加载类,只有不能自动载入时,才需要
        $form    = new Application_Form_Guestbook;//实例化这个方法

        if ($this->getRequest()->isPost()) {//如果是POST传递的结果
            if ($form->isValid($request->getPost())) {//判断传递是否有效
                $comment = new Application_Model_Guestbook($form->getValues());
                $mapper  = new Application_Model_GuestbookMapper();
                $mapper->save($comment);
                return $this->_helper->redirector('index');
            }
        }

        $this->view->form = $form;//将表单赋值给试图
    }
}

最后添加一个简单的sign视图文件即可:
地址:application/views/scripts/guestbook/sgin.php
复制代码 代码如下:

Please use the form below to sign our guestbook!
$this->form->setAction($this->url());
echo $this->form;

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

PHP does not recognize ZendOptimizer, how to solve it? PHP does not recognize ZendOptimizer, how to solve it? Mar 19, 2024 pm 01:09 PM

PHP does not recognize ZendOptimizer, how to solve it? In PHP development, sometimes you may encounter a situation where PHP cannot recognize ZendOptimizer, which will cause some PHP codes to not run properly. In this case, we need to take some measures to solve the problem. Some possible workarounds are described below, along with specific code examples. 1. Confirm whether ZendOptimizer is installed correctly: First, we need to confirm that ZendOptimizer

How to configure the Window2003 IIS+MySQL+PHP+Zend environment How to configure the Window2003 IIS+MySQL+PHP+Zend environment Jun 02, 2023 pm 09:56 PM

The Windows 2003 installation package includes Zend, PHP5.2.17, PHPWind8.7 and PHPMyadmin3.5.2. You can download the installation package directly to save time searching for resources. However, since MySQL has exceeded the upload limit, you need to go to the MySQL official website to download. Then unzip and copy to the D drive, as shown below: MySQLinDdisk Install and configure WindowsIIS+FTP Click Start>Control Panel>Add or Remove Programs.AddingordeletingaPG Click Add/Remove Windows Components (A). Addingorde

How to use the PHP framework Zend to develop an efficient ERP management platform How to use the PHP framework Zend to develop an efficient ERP management platform Jun 26, 2023 pm 11:00 PM

With the rapid development of information technology, more and more enterprises are beginning to realize the necessity of information management. ERP (Enterprise Resource Planning) management platform is an important tool for modern enterprise management, which can help enterprises realize resource planning, collaboration, control, optimization and management. Among them, the PHP framework Zend is an excellent development tool that can help developers develop ERP systems quickly and efficiently. This article will introduce how to use Zend to develop an efficient ERP management platform. 1. Determine requirements analysis before starting the development process

Laravel vs Zend: Which framework is better for developing large applications? Laravel vs Zend: Which framework is better for developing large applications? Jun 19, 2023 am 08:52 AM

With the continuous development of Internet applications, the demand for the development of large-scale applications is also increasing. In this context, it is particularly important to choose a development framework that suits you. Laravel and Zend are two widely used PHP frameworks. They each have their own advantages, but which one is more suitable for developing large-scale applications? Laravel is a popular development framework that has become one of the preferred frameworks for PHP developers. It adopts a modern design concept and has a variety of powerful built-in functions and tools, such as EloquentOR

Symfony 3 vs Zend Framework 3: Which PHP framework is easier to get started with? Symfony 3 vs Zend Framework 3: Which PHP framework is easier to get started with? Jun 19, 2023 am 09:46 AM

PHP is a widely used dynamic web programming language. Developers can make use of different frameworks to simplify their web development work. Symfony and ZendFramework are one of the two most popular frameworks in PHP. Beginners are often confused when choosing between Symfony3 and ZendFramework3. Here we will compare these two frameworks to see which one is easier to get started with. Symfony3Symfony is a PH based on the MVC model

Develop a high-performance search engine using the PHP framework Zend Develop a high-performance search engine using the PHP framework Zend Jun 27, 2023 am 08:36 AM

With the explosive growth of Internet information, search engines have become one of the preferred ways for people to obtain information. Now, as the number of websites continues to increase, the rapid response and accuracy of search engines have become more and more important, which requires search engines to have high performance. In this article, I will introduce how to use the PHP framework Zend to develop a high-performance search engine. 1. Why use Zend Framework? Zend Framework is a high-performance PHP framework that has excellent performance and scalability.

See all articles