php 从 Zend Framework 2 中的路由获取 $_GET 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11820889/
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 02:12:10  来源:igfitidea点击:

Getting $_GET parameters from route in Zend Framework 2

phpzend-frameworkroutingzend-framework2superglobals

提问by Swader

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

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

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

网址示例 mypage.com/mymodule/mycontroller/myaction/someparam/5:

ZF1

ZF1

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

ZF2

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.

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

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

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

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

调用$_GET = $this->getEvent()->getRouteMatch()->getParam('someparam');我的每个控制器都可以,但我不想要那样。

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.

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

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.

编辑:看起来我还不够清楚,所以我会尽量澄清一些。我希望我通过 /key/value 格式在 URL 中输入的任何参数都可以立即在 $_GET 中使用。我在获取参数方面真的没有问题,我知道如何获取它并且我扩展了 Zend 的控制器,所以我可以像在 ZF1 中一样再次调用 $this->getParams,现在所有控制器都扩展了那个,我只是想要URL 中的参数也自动位于 $_GET 中,因此我可以在本机使用 $_GET 的第三方组件中轻松访问它们。

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.

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

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

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

回答by eli_daxs

In ZF2, Im using this

在 ZF2 中,我使用这个

$getparams = $this->getRequest()->getQuery();

回答by Samuel Herzog

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

首先,$_GET如果您在面向对象的堆栈上构建,则不应直接使用或任何其他超全局变量。SRP 以这种方式失效。

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 $_GETwith a simple loop.

如果您无法更改(第 3 方?)库的方式进行更改,您可能想要挂钩 MvcEvent,收听 --event--,然后获取RouteMatch,您可以填充$_GET一个简单的循环。

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

回答by Nicolas Finelli

You could use in your controlller:

您可以在控制器中使用:

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

Regards

问候