使用Zend框架处理输入(Post,get等) [英] Handling input with the Zend Framework (Post,get,etc)

查看:292
本文介绍了使用Zend框架处理输入(Post,get等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im在Zend代码中重新分解PHP,并且所有代码都充满了 $ _ GET [this] $ _ POST [ ] 。我一直使用更多phpish $ this-> _request-> getPost('this') $ this-> _request-> ; getQuery('that')(这与getGet的getquery替代逻辑不同)。

所以我想知道是否我的方法更安全/更好/更容易维护。我在Zend Framework文档中读到,您必须验证您自己的输入,因为请求对象不会这样做。



这给我留下了两个问题:


  • ? (或者更好的方法)

  • 用这种方法验证php输入的最佳实践是什么?



感谢!

解决方案

我通常使用$ this - > _ request-> getParams();检索帖子或网址参数。然后,我使用 Zend_Filter_Input 进行验证和过滤。 getParams()不会进行验证。使用Zend_Filter_Input,您可以使用Zend Validators进行应用程序级验证(或者您也可以自己编写)。例如,您可以确保'months'字段是一个数字:

  $ data = $ this-> _request- > getParams()方法; 

$ validators = array(
'month'=>'Digits',
);

$ input = new Zend_Filter_Input($ filters,$ validators,$ data);


im re-factoring php on zend code and all the code is full of $_GET["this"] and $_POST["that"]. I have always used the more phpish $this->_request->getPost('this') and $this->_request->getQuery('that') (this one being not so much logical with the getquery insteado of getGet).

So i was wondering if my method was safer/better/easier to mantain. I read in the Zend Framework documentation that you must validate your own input since the request object wont do it.

That leaves me with 2 questions:

  • What is best of this two? (or if theres another better way)
  • What is the best practice for validating php input with this methods?

Thanks!

解决方案

I usually use $this->_request->getParams(); to retrieve either the post or the URL parameters. Then I use the Zend_Filter_Input to do validation and filtering. The getParams() does not do validation.

Using the Zend_Filter_Input you can do application level validation, using the Zend Validators (or you can write your own too). For example, you can make sure the 'months' field is a number:

$data = $this->_request->getParams();

$validators = array(
    'month'   => 'Digits',
);

$input = new Zend_Filter_Input($filters, $validators, $data);

这篇关于使用Zend框架处理输入(Post,get等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆