文档章节

A Practical Tutorial Of Zend Framework(四)

Donald Yang
 Donald Yang
发布于 2016/06/20 19:13
字数 606
阅读 2
收藏 0

Zend_InputFilter

The last component this tutorial covers is Zend_InputFilter. This class provides a simple but rigid approach to input filtering. You instantiate it by passing an array of data to be filtered:
<?php

$filterPost 
= new Zend_InputFilter($_POST);

?>
This sets the array ( $_POST) to NULL, so direct access is no longer possible. Zend_InputFilter instead provides a small, focused collection of methods that filter data according to specific criteria. For example, if you want the alphabetic characters of $_POST['name'], you can use the getAlpha() method:
<?php

/* $_POST['name'] = 'John123Doe'; */

$filterPost = new Zend_InputFilter($_POST);

/* $_POST = NULL; */

$alphaName $filterPost->getAlpha('name');

/* $alphaName = 'JohnDoe'; */

?>
The argument you pass each filtering method is the key that corresponds to the array element to be filtered. The object ( $filterPost in this example) is a protected cage that contains the tainted data, making access to that data more controlled and consistent. Therefore, you should always use Zend_InputFilter when you need to access input.
Note: Zend_Filter provides static filtering methods that follow the same conventions as the Zend_InputFilter methods.

Building a News Management System

Although the preview release contains many more components (and even more are being developed), the components already discussed provide all you need to build a simple application. In the process, you should gain a clearer understanding of the framework's basic structure and design. Everyone develops applications a bit differently, and the Zend Framework tries to embrace diversity as much as possible. Similarly, this tutorial is subject to my preferences, so please adjust these to suite your own tastes. When I begin developing an application, I start with the interface. This doesn't mean I spend hours with markup, stylesheets, and images, but I do approach the problem from the perspective of a user. As such, I see an application as a collection of pages, where each page is a unique URL. This news management system consists of the following URLs:
/
/add/news
/add/comment
/admin
/admin/approve
/view/{id}
You want to immediately begin thinking of these URLs in terms of controllers. The IndexController lists the news, the AddController handles adding news and comments, the AdminController handles administrative actions such as approving news, and the ViewController handles viewing a specific news entry and its corresponding comments. Begin by removing FooController.php if it still exists, and modify IndexController.php to add the appropriate actions and some comments as placeholders for the business logic:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
IndexController extends Zend_Controller_Action 
{
    public function 
indexAction()
    {
        
/* List the news. */
    
}

    public function 
noRouteAction()
    {
        
$this->_redirect('/');
    }
}

?>
Next, create AddController.php:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
AddController extends Zend_Controller_Action
{
    function 
indexAction()
    {
        
$this->_redirect('/');
    }

    function 
commentAction()
    {
        
/* Add a comment. */
    
}

    function 
newsAction()
    {
        
/* Add news. */
    
}

    function 
__call($action$arguments)
    {
        
$this->_redirect('/');
    }
}

?>
Note that the indexAction() method of AddController should never be called. This only happens when the requested path is /add. Because a user might explore the URLs manually, this is likely, so you can redirect the user to the front page, display an error, or take whatever action you feel is appropriate. Next, create AdminController.php:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
AdminController extends Zend_Controller_Action
{
    function 
indexAction()
    {
        
/* Display admin interface. */
    
}

    function 
approveAction()
    {
        
/* Approve news. */
    
}

    function 
__call($action$arguments)
    {
        
$this->_redirect('/');
    }
}

?>
Finally, create ViewController.php:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
ViewController extends Zend_Controller_Action
{
    function 
indexAction()
    {
        
$this->_redirect('/');
    }

    function 
__call($id$arguments)
    {
        
/* Display news and comments for $id. */
    
}
}

?>
As with AddController, the index() method should never be called, so you can take whatever action you feel is appropriate. ViewController is a bit different than the others, because you don't know what the valid actions are. In order to support URLs like /view/23, you must support dynamic actions with __call().

本文转载自:http://blog.csdn.net/ffyd2000/article/details/668076

Donald Yang
粉丝 2
博文 56
码字总数 139
作品 0
成都
私信 提问
菜菜鸟Zend Framework 2 不完全学习涂鸦(一)-- 安装配置

声明:本人 PHPer 菜鸟一枚,Zend Framework 2 框架的菜菜鸟,刚学习 ZF2 不过 2 天。由于英语实在太破,所以一开始只能找网上 ZF2 中文教程,但很多教程都只说到 ZF2 安装与配置就结束了,没...

Jacky.Wang
2013/07/18
23.3K
27
世界500强企业招聘php开发人员

Job description 1. Maintain, Develop, implement and test server-side and front-end PHP-based applications 2. Maintain, Develop, implement Java applications 3. Provide technical ......

时海连camilla
2011/12/21
393
5
菜菜鸟Zend Framework 2 不完全学习涂鸦(十四)-- 单元测试一个ZF2应用程序

单元测试一个Zend Framework 2应用程序 在一个大型项目的开发过程中特别是牵涉到许多人员参与时,一个可靠的单元测试是必不可少的。对于应用程序每当有所变化后都返回并手动对每个组件进行测...

Jacky.Wang
2013/08/23
3.4K
1
资讯门户内容管理系统

具体要求: 服务商擅长开发类型: 资讯门户、内容管理系统 一、软件功能及用途: 资讯门户管理系统 二、核心要求: 1.使用zend framework2.4.9或3.0框架,PHP7x+MYSQL5.7; 2.开发者需有zen...

medianews
2017/02/08
7
0
国外程序员推荐的免费编程书籍资源

StackOverflow 上有位网友(xenoterracide)提问: 我正试着搜集整理一个可在网上免费阅读的计算机编程书籍列表。这些书可以是某种特定编程语言,也可以计算机方面通用书籍。网上有哪些免费可...

职通网
2011/11/07
676
1

没有更多内容

加载失败,请刷新页面

加载更多

OpenStack 简介和几种安装方式总结

OpenStack :是一个由NASA和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目。项目目标是提供实施简单、可大规模扩展、丰富、标准统一的云计算管理平台。OpenSta...

小海bug
昨天
6
0
DDD(五)

1、引言 之前学习了解了DDD中实体这一概念,那么接下来需要了解的就是值对象、唯一标识。值对象,值就是数字1、2、3,字符串“1”,“2”,“3”,值时对象的特征,对象是一个事物的具体描述...

MrYuZixian
昨天
6
0
数据库中间件MyCat

什么是MyCat? 查看官网的介绍是这样说的 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务、ACID、可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵...

沉浮_
昨天
7
0
解决Mac下VSCode打开zsh乱码

1.乱码问题 iTerm2终端使用Zsh,并且配置Zsh主题,该主题主题需要安装字体来支持箭头效果,在iTerm2中设置这个字体,但是VSCode里这个箭头还是显示乱码。 iTerm2展示如下: VSCode展示如下: 2...

HelloDeveloper
昨天
9
0
常用物流快递单号查询接口种类及对接方法

目前快递查询接口有两种方式可以对接,一是和顺丰、圆通、中通、天天、韵达、德邦这些快递公司一一对接接口,二是和快递鸟这样第三方集成接口一次性对接多家常用快递。第一种耗费时间长,但是...

程序的小猿
昨天
11
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部