在Zend Framework 2中从路由获取$ _GET参数 [英] Getting $_GET parameters from route in Zend Framework 2

查看:123
本文介绍了在Zend Framework 2中从路由获取$ _GET参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend Framework 1有一种非常简单的方法来解析URL路由并在$ _GET超全局变量中设置找到的参数以便于访问。当然,您可以在控制器内使用-> getParam($ something),但是如果在URL中找到了参数,则可以通过$ _GET 对其进行访问。



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'] //未定义的索引someparam

显然,区别在于ZF2不会将路由参数放入$ _GET超全局变量中。



我如何使其将已解析的参数放入$ _GET超全局变量中,因为扩展了控制器,只是定义一个构造函数,这是不可能的(因为RouteMatch还不是一个对象,并且不能从控制器的构造函数调用)?



调用 $ _ GET = $ this-> getEvent()-> getRouteMatch()-> getParam('someparam'); 在我的每个控制器中都可以使用,但是我没有



换句话说,按照上面的示例URL,我希望能够执行$ _GET ['someparam']并仍然获得该值应用程序中任何组件中的 5。



编辑:看起来我不够清楚,所以我将尝试阐明更多内容。我希望通过/ key / value格式输入URL的任何参数都可以立即在$ _GET中使用。我对获取参数确实没有问题,我知道如何获取它,并且我扩展了Zend的控制器,所以我可以像在ZF1中一样再次调用$ this-> getParams,现在所有控制器都扩展了该参数,我只想网址中的参数也将自动包含在$ _GET中,因此我可以在本地使用$ _GET的第三方组件中轻松访问它们。



编辑2:更新为对Samuel Herzog答案的反应:
在这种情况下,我真的不介意使SRP无效,因为库的构建方式是它们需要直接访问$ _GET-它们进行自己的过滤并直接依赖这个超全球他们还直接获取$ _FILES和$ _POST进行处理,这就是他们的代码的工作方式。



我在抽象控制器中做了以下方法:
$ this-> mergeGet();这基本上使$ _GET吸收了所有路由匹配的参数,并且一切都按预期工作,但是由于在每个控制器/动作中都需要使用库,因此每次调用该方法都可能很麻烦。如果只有控制器具有init()方法,如ZF1中那样...

解决方案

首先,您不应该使用 $ _ GET 或任何其他超全局变量(如果要在面向对象的堆栈上构建)。 SRP通过这种方式无效。



如果您无法更改(第3方?)库的更改方式,则可能要挂接到MvcEvent中,收听--event--,然后获取 RouteMatch ,您可以使用简单的循环填充 $ _ GET 。 / p>

对于最高性能的答案,您应该知道是否需要为每个动作(仅一个模块)或仅在某些控制器/动作中使用命名库。
如果最新的是您的用例,则应该编写一个控制器插件。



第一种方法的一些示例代码:

 命名空间YourModule; 
使用Zend\EventManager\EventInterface作为事件;
使用Zend\Mvc\MvcEvent;

类模块
{
...

公共功能onBootstrap(事件$ 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;
}
}
});
}
}


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 also accessible via $_GET.

Example for 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 the route params into the $_GET superglobal.

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)?

Calling $_GET = $this->getEvent()->getRouteMatch()->getParam('someparam'); in every one of my controllers would work, but I don't want that.

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.

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.

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.

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

解决方案

First of all, you shouldn't use $_GET or any other superglobal directly if you're building on an object oriented stack. The SRP is invalidated this way.

If you have no possibility to change the way of your (3rd party?) librarys to change you might want to hook into the MvcEvent, listen to --event-- and then get the RouteMatch, you may fill $_GET with a simple loop.

For a most-performant answer, you should know if the named library will be needed for every action, just for one module, or only in certain controllers/actions. If the latest is your use-case, you should write a controller plugin instead.

some example 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;
                }
            }
        });
    }
}

这篇关于在Zend Framework 2中从路由获取$ _GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆