Getting $ _GET parameters from a route in Zend Framework 2

Zend Framework 1 had a very simple way of analyzing URL routes and setting the parameters found in the $ _GET superglobal file for easy access. Of course, you can use β†’ getParam ($ something) inside the controller, but if the parameter was found in the URL, it was also accessible via $ _GET.

Example for the URL mypage.com/mymodule/mycontroller/myaction/someparam/5:

Zf1

 $this->getParam('someparam'); // 5 $_GET['someparam']; // 5 

Zf2

 $this->getEvent()->getRouteMatch()->getParam('someparam'); // 5 $_GET['someparam'] // undefined index someparam 

Obviously, the difference is that ZF2 does NOT put route parameters into the superglobal value of $ _GET.

How to make it analyze the parameters in the $ _GET supergalbal, since the extension of the controller and just defining a constructor that does this is out of the question (since RouteMatch is not an object yet and cannot be called from the controller's constructor)?

Call $_GET = $this->getEvent()->getRouteMatch()->getParam('someparam'); will work in each of my controllers, but I do not want this.

In other words, following the example URL above, I want to be able to do $ _GET ['someparam'] and still get the value "5" in any component of the application.

Edit: It seems I was not clear enough, so I will try to clarify something else. I want any parameter that I entered in the URL via form / key / value to be available in $ _GET instantly. I really have no problem getting the parameter, I know how to get it, and I expanded the Zend controller, so I can just call $ this-> getParams again, as in ZF1, and now all the controllers expand this one, I just want the parameters from URLs will automatically be in $ _GET, so I can easily access them in third-party components that use $ _GET natively.

Edit 2: Updated as a reaction to Samuel Herzog: Actually, I do not mean SRP invalidity in this case, because libraries are built in such a way that they need direct access to $ _GET - they perform their own filtering and are directly dependent on this superglobal. They also directly extract $ _FILES and $ _POST for processing, this is exactly how their code works.

In the abstract controller, I made the following method: $ This-> mergeGet (); which basically causes $ _GET to absorb all the parameters matched by the route, and everything works as intended, but seeing that the libraries will be needed in each controller / action, it may require a tedious call to this method every time. If only the controller had an init () method, as in ZF1 ...

+6
source share
3 answers

First of all, you should not use $_GET or any other superglobal values ​​if you are building an object-oriented stack. SRP is not valid in this way.

If you don’t have the opportunity to change the way you change your libraries (third-party ??), you can connect to MvcEvent, listen to -event-- and then get RouteMatch , you can fill in $_GET with a simple loop.

For the most efficient answer, you should know if a named library is needed for each action, only for one module, or only in certain controllers / actions. If the latter is your use case, you should write a controller plugin.

some sample code for the first approach:

 namespace YourModule; use Zend\EventManager\EventInterface as Event; use Zend\Mvc\MvcEvent; class Module { ... public function onBootstrap(Event $ev) { $application = $e->getApplication(); $eventManager = $application->getEventManager(); $eventManager->attach('route', function(MvcEvent $mvcEvent) { $params = $mvcEvent->getRouteMatch()->getParams(); foreach ( $params as $name => $value ) { if ( ! isset($_GET[$name]) { $_GET[$name] = $value; } } }); } } 
+7
source

In ZF2, Im using this

 $getparams = $this->getRequest()->getQuery(); 
+13
source

You can use in your controller:

  $paramValue = $this->params()->fromQuery('your_param_here'); 

Hi

+1
source

Source: https://habr.com/ru/post/922179/


All Articles