php Zend 框架:我可以只获取 GET 参数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3830856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zend Framework: Can i just get GET params?
提问by Jiew Meng
In Zend Framework, most of the time to get a param, i will use
在 Zend Framework 中,大部分时间获取参数,我会使用
// from controller
$this->getRequest()->getParam('key');
but how can i get just GET params using the 'Zend' way? Or do i just use $_GET
? Is there any difference between
但是我怎样才能使用“Zend”方式获得 GET 参数呢?还是我只是使用$_GET
?有什么区别吗
$this->getRequest()->getParam('key');
vs
对比
$_GET['key'];
回答by Ryan Chouinard
Use getQuery()
:
使用getQuery()
:
$this->_request->getQuery('key');
Other methods available include
其他可用的方法包括
- getParam()
- getQuery()
- getPost()
- getCookie()
- getServer()
- getEnv()
- 获取参数()
- 获取查询()
- 获取邮局()
- 获取Cookie()
- 获取服务器()
- 获取环境()
getParam()
checks user params first, then $_GET, and then $_POST, returning the first match found or null.
getParam()
首先检查用户参数,然后是 $_GET,然后是 $_POST,返回找到的第一个匹配项或 null。
Try to avoid accessing the superglobals directly.
尽量避免直接访问超全局变量。
回答by Gordon
The main difference is that
主要区别在于
$_GET['key'];
is a dependency on the environment. It requires the superglobal to be available and containing a key of that name. It's also just a simple array access, while
是对环境的依赖。它要求超全局变量可用并包含该名称的键。它也只是一个简单的数组访问,而
$this->getRequest()->getParam('key');
is an API method call. Access to the Request is abstracted. There is no dependency on the actual environment. The Request object could be a mock. The getParam
method will always return a value regardless whether it is from $_GET
or $_POST
.
是一个 API 方法调用。对请求的访问是抽象的。不依赖于实际环境。Request 对象可能是一个模拟对象。该getParam
方法将始终返回一个值,无论它是 from$_GET
还是$_POST
。
Putting an abstraction on top of the Request is better, because it allows for more decoupling, less dependencies and therefor makes your application easier to test and maintain.
将抽象放在 Request 之上会更好,因为它允许更多的解耦,更少的依赖,从而使您的应用程序更易于测试和维护。
回答by BlueBird
This works for ZF2
这适用于 ZF2
$this->params()->fromQuery('key', 1); // second argument is optional default paramter
回答by algorhythm
In Zend Framework 1 there are two possibilities to define "visible" parameters.
在 Zend Framework 1 中有两种定义“可见”参数的可能性。
https://subdomain.domain.tld(/module)/controller/name/parameter1/value1https://subdomain.domain.tld(/module)/controller/name/?parameter2=value2
https://subdomain.domain.tld(/module)/controller/name/parameter1/value1 https://subdomain.domain.tld(/module)/controller/name/?parameter2=value2
First parameter1
is part of the URL path and second parameter2
is a real GET
parameter. Both will be returned if calling $request->getParams()
. But only parameter2
is returned when using $request->getQuery()
. Because parameter1
is not part of the query part of the url, logical.
第一个parameter1
是 URL 路径的一部分,第二个parameter2
是实际GET
参数。如果调用 ,两者都将返回$request->getParams()
。但仅parameter2
在使用时返回$request->getQuery()
。因为parameter1
不是url的查询部分,合乎逻辑。
Now, I like the answer of Ryan Chouinard
how to get the parameter2
with $request->getQuery()
. But parameter1
behaves like a GET
parameter and I want to treat them like that. So how can I get the visible parameter1
and parameter2
but not the additional hidden parameter3
from the post data?
现在,我喜欢Ryan Chouinard
如何获得parameter2
with的答案$request->getQuery()
。但是parameter1
表现得像一个GET
参数,我想这样对待它们。所以,我怎么能得到的有形parameter1
和parameter2
而不是额外的隐藏parameter3
在后的数据?
My only solution is a helper that clones the request and changes the so called paramSources
(default: ['_GET', '_POST']
) to ['_GET']
and then use getParam()
as regular...
我唯一的解决方案是一个帮手克隆的要求和改变所谓的paramSources
(默认['_GET', '_POST']
)来['_GET']
,然后用getParam()
作为普通...
/**
* @param Zend_Controller_Request_Http $request
* @param string $param
* @param mixed|null $default
*
* @return mixed|null
*/
function getVisibleParam(string $param, $default = null, Zend_Controller_Request_Http $request = null)
{
if (
!$request &&
!($request = Zend_Controller_Front::getInstance()->getRequest())
) {
throw new \RuntimeException('There is no request. Are you in a wrong context?');
}
$_request = clone $request;
$_request->setParamSources(['_GET']);
return $_request->getParam($param, $default);
}
回答by Tigerman55
After studying Zend 2's in depth data binding documentation, I've found that it is best to access parameters from the route via the automatically accessible Params plugin. Utilizing this plugin, you can get a parameter as shown below from within a controller.
在研究了Zend 2 的深入数据绑定文档后,我发现最好通过可自动访问的Params 插件从路由中访问参数。使用此插件,您可以从控制器中获取如下所示的参数。
$this->params('key');