Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting $_GET parameters from route in Zend Framework 2
    primarykey
    data
    text
    <p>Zend Framework 1 had a very simple way of parsing URL routes and setting found params in the $_GET superglobal for easy access. Sure, you could use ->getParam($something) inside the controller, but if the param was found in the URL, it was <strong>also</strong> accessible via $_GET. </p> <p>Example for url <code>mypage.com/mymodule/mycontroller/myaction/someparam/5:</code></p> <p>ZF1</p> <pre><code>$this-&gt;getParam('someparam'); // 5 $_GET['someparam']; // 5 </code></pre> <p>ZF2 </p> <pre><code>$this-&gt;getEvent()-&gt;getRouteMatch()-&gt;getParam('someparam'); // 5 $_GET['someparam'] // undefined index someparam </code></pre> <p>Obviously, the difference is that ZF2 does NOT put the route params into the $_GET superglobal.</p> <p>How do I make it put the parsed parameters into the $_GET superglobal, since extending the controller and just defining a constructor that does that is out of the question (because RouteMatch is not an object yet and cannot be called from the controller's constructor)?</p> <p>Calling <code>$_GET = $this-&gt;getEvent()-&gt;getRouteMatch()-&gt;getParam('someparam');</code> in every one of my controllers would work, but I don't want that.</p> <p>In other words, following the example URL from above, I want to be able to do $_GET['someparam'] and still get the value "5" in any component in the application.</p> <p>Edit: Looks like I wasn't clear enough, so I'll try to clarify some more. I want whatever param I enter in the URL via /key/value formation to be available in $_GET instantly. I don't really have a problem with getting the param, I know how to get it and I extended Zend's controller so I can just call $this->getParams again like in ZF1, and now all controllers extend that one, I just want the params from the URL to automatically be in $_GET as well, so I can access them easily in third party components which use $_GET natively.</p> <p>Edit 2: Updated as reaction to Samuel Herzog's answer: I don't really mind invalidating the SRP in this case, because the libraries are built in such a way that they need direct access to $_GET - they do their own filtering and directly depend on this superglobal. They also directly fetch $_FILES and $_POST for processing, it's just the way their code works.</p> <p>I've made the following method in the abstract controller: $this->mergeGet(); which basically makes $_GET absorb all the route matched params and everything works as intended, but seeing as the libraries will be required in every controller/action, it might get tedious to call that method every time. If only the controller had an init() method like in ZF1...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload