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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:15:15  来源:igfitidea点击:

Zend Framework: Can i just get GET params?

phpzend-framework

提问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 getParammethod will always return a value regardless whether it is from $_GETor $_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 parameter1is part of the URL path and second parameter2is a real GETparameter. Both will be returned if calling $request->getParams(). But only parameter2is returned when using $request->getQuery(). Because parameter1is 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 Chouinardhow to get the parameter2with $request->getQuery(). But parameter1behaves like a GETparameter and I want to treat them like that. So how can I get the visible parameter1and parameter2but not the additional hidden parameter3from the post data?

现在,我喜欢Ryan Chouinard如何获得parameter2with的答案$request->getQuery()。但是parameter1表现得像一个GET参数,我想这样对待它们。所以,我怎么能得到的有形parameter1parameter2而不是额外的隐藏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');