开发者

Zend Framework: Can i just get GET params?

In Zend Framework, most of the time to get a param, i will use

// from con开发者_如何学运维troller
$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

$this->getRequest()->getParam('key');

vs

$_GET['key'];


Use getQuery():

$this->_request->getQuery('key');

Other methods available include

  • getParam()
  • getQuery()
  • getPost()
  • getCookie()
  • getServer()
  • getEnv()

getParam() checks user params first, then $_GET, and then $_POST, returning the first match found or null.

Try to avoid accessing the superglobals directly.


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.

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.


This works for ZF2

$this->params()->fromQuery('key', 1); // second argument is optional default paramter


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.

$this->params('key');


In Zend Framework 1 there are two possibilities to define "visible" parameters.

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. It's part of the url, logical.

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?

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...

/**
 * @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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜