如何在Zend Framework 2中访问路由,发布,获取等参数 [英] How to access route, post, get etc. parameters in Zend Framework 2

查看:499
本文介绍了如何在Zend Framework 2中访问路由,发布,获取等参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得zf2中与页面请求相关的各种参数?像post / get参数,被访问的路由,发送头文件和文件上传。使用 Params插件,引入在beta5。它具有实用的方法,使访问不同类型的参数很容易。一如既往,阅读测试可以证明有用的东西是应该被使用的。



获取单个值



在控制器中的一个命名参数中,你需要为你正在查找的参数类型选择合适的方法并传入名字。



例子: h3>

  $ this-> params() - > fromPost('paramname'); //从POST 
$ this-> params() - > fromQuery('paramname'); //从GET
$ this-> params() - > fromRoute('paramname'); //从RouteMatch
$ this-> params() - > fromHeader('paramname'); //从头文件
$ this-> params() - > fromFiles('paramname'); //上传文件

 



< h2>默认值



所有这些方法都支持缺省值,如果没有找到给定名称的参数,将返回默认值。

示例:



  $ orderBy = $ this-> params () - > fromQuery('orderby','name'); 

访问 http://example.com/?orderby=birthdate
$ orderBy 将具有值 birthdate

访问 http://example.com/ 时,
$ orderBy 将具有默认值 名称 。 >

获取所有参数



要获取一种类型的所有参数,只要不传入任何内容,Params插件以名称作为键返回值的数组。

示例:



  $ allGetValues = $ this-> params () - > fromQuery(); //空方法调用

访问 http://example.com/?orderby=birthdate&filter=hasphone $ allGetValues 将是一个像 p>

 数组(
'orderby'=>'birthdate',
'filter'=>'hasphone ',
);

 



不使用Params插件



如果您检查源代码,你会发现它只是一个简单的包装其他控制器,以允许更一致的参数检索。如果您由于某种原因需要直接访问它们,您可以在源代码中看到如何完成。



示例:



  $ this-> getRequest() - > getRequest('name','default'); 
$ this-> getEvent() - > getRouteMatch() - > getParam('name','default');

注意:您可以使用超全局变量$ _GET,$ _POST等。但是,这是令人沮丧的。


How can I get various parameters related to the page request in zf2? Like post/get parameters, the route being accessed, headers sent and files uploaded.

解决方案

The easiest way to do that would be to use the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the tests can prove valuable to understand how something is supposed to be used.

Get a single value

To get the value of a named parameter in a controller, you will need to select the appropriate method for the type of parameter you are looking for and pass in the name.

Examples:

$this->params()->fromPost('paramname');   // From POST
$this->params()->fromQuery('paramname');  // From GET
$this->params()->fromRoute('paramname');  // From RouteMatch
$this->params()->fromHeader('paramname'); // From header
$this->params()->fromFiles('paramname');  // From file being uploaded

 

Default values

All of these methods also support default values that will be returned if no parameter with the given name is found.

Example:

$orderBy = $this->params()->fromQuery('orderby', 'name');

When visiting http://example.com/?orderby=birthdate, $orderBy will have the value birthdate.
When visiting http://example.com/, $orderBy will have the default value name.
 

Get all parameters

To get all parameters of one type, just don't pass in anything and the Params plugin will return an array of values with their names as keys.

Example:

$allGetValues = $this->params()->fromQuery(); // empty method call

When visiting http://example.com/?orderby=birthdate&filter=hasphone $allGetValues will be an array like

array(
    'orderby' => 'birthdate',
    'filter'  => 'hasphone',
);

 

Not using Params plugin

If you check the source code for the Params plugin, you will see that it's just a thin wrapper around other controllers to allow for more consistent parameter retrieval. If you for some reason want/need to access them directly, you can see in the source code how it's done.

Example:

$this->getRequest()->getRequest('name', 'default');
$this->getEvent()->getRouteMatch()->getParam('name', 'default');

NOTE: You could have used the superglobals $_GET, $_POST etc., but that is discouraged.

这篇关于如何在Zend Framework 2中访问路由,发布,获取等参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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