php 如何在 Zend Framework 2 中访问路由、发布、获取等参数

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

How to access route, post, get etc. parameters in Zend Framework 2

phpfile-uploadzend-routezend-framework2

提问by Matsemann

How can I get various parameters related to the page request in zf2? Like post/get parameters, the route being accessed, headers sent and files uploaded.

如何在 zf2 中获取与页面请求相关的各种参数?像 post/get 参数,正在访问的路由,发送的标题和上传的文件。

回答by Matsemann

The easiest way to do that would be to use the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the testscan prove valuable to understand how something is supposed to be used.

最简单的方法是使用在 beta5 中引入的Params 插件。它具有实用方法,可以轻松访问不同类型的参数。与往常一样,阅读测试对于理解应该如何使用某些东西很有价值。

Get a single value

获取单个值

To get the value of a named parameter in a controller, you will need to select the appropriate method for the type of parameter you are looking for and pass in the name.

要获取控制器中命名参数的值,您需要为要查找的参数类型选择适当的方法并传入名称。

Examples:

例子:

$this->params()->fromPost('paramname');   // From POST
$this->params()->fromQuery('paramname');  // From GET
$this->params()->fromRoute('paramname');  // From RouteMatch
$this->params()->fromHeader('paramname'); // From header
$this->params()->fromFiles('paramname');  // From file being uploaded

 

 

Default values

默认值

All of these methods also support default values that will be returned if no parameter with the given name is found.

所有这些方法还支持默认值,如果没有找到具有给定名称的参数,将返回这些默认值。

Example:

例子:

$orderBy = $this->params()->fromQuery('orderby', 'name');

When visiting http://example.com/?orderby=birthdate, $orderBywill have the value birthdate.
When visiting http://example.com/, $orderBywill have the defaultvalue name.
 

当访问http://example.com/?orderby=birthdate 时$orderBy的值为birthdate
访问http://example.com/ 时$orderBy将具有默认name
 

Get all parameters

获取所有参数

To get all parameters of one type, just don't pass in anything and the Params plugin will return an array of values with their names as keys.

要获取一种类型的所有参数,只需不要传入任何内容,Params 插件将返回一个以名称为键的值数组。

Example:

例子:

$allGetValues = $this->params()->fromQuery(); // empty method call

When visiting http://example.com/?orderby=birthdate&filter=hasphone$allGetValueswill be an array like

当访问http://example.com/?orderby=birthdate&filter=hasphone $allGetValues将是一个数组

array(
    'orderby' => 'birthdate',
    'filter'  => 'hasphone',
);

 

 

Not using Params plugin

不使用参数插件

If you check the source codefor the Params plugin, you will see that it's just a thin wrapper around other controllers to allow for more consistent parameter retrieval. If you for some reason want/need to access them directly, you can see in the source code how it's done.

如果您检查Params 插件的源代码,您会发现它只是其他控制器的薄包装器,以允许更一致的参数检索。如果您出于某种原因想要/需要直接访问它们,您可以在源代码中看到它是如何完成的。

Example:

例子:

$this->getRequest()->getRequest('name', 'default');
$this->getEvent()->getRouteMatch()->getParam('name', 'default');

NOTE:You could have used the superglobals $_GET, $_POST etc., but that is discouraged.

注意:您可以使用超级全局变量 $_GET、$_POST 等,但不鼓励这样做。

回答by Susy11

The easisest way to get a posted json string, for example, is to read the contents of 'php://input' and then decode it. For example i had a simple Zend route:

例如,获取已发布的 json 字符串的最简单方法是读取 'php://input' 的内容,然后对其进行解码。例如,我有一个简单的 Zend 路线:

'save-json' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route'    => '/save-json/',
                'defaults' => array(
                    'controller' => 'CDB\Controller\Index',
                    'action'     => 'save-json',
                ),
            ),
        ),

and i wanted to post data to it using Angular's $http.post. The post was fine but the retrive method in Zend

我想使用 Angular 的 $http.post 向它发布数据。帖子很好,但 Zend 中的检索方法

$this->params()->fromPost('paramname'); 

didn't get anything in this case. So my solution was, after trying all kinds of methods like $_POST and the other methods stated above, to read from 'php://':

在这种情况下没有得到任何东西。所以我的解决方案是,在尝试了各种方法(如 $_POST 和上述其他方法)后,从 'php://' 读取:

$content = file_get_contents('php://input');
print_r(json_decode($content));

I got my json array in the end. Hope this helps.

最后我得到了我的 json 数组。希望这可以帮助。

回答by Ankit Vishwakarma

require_once 'lib/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));

$loader->registerNamespace('Http\PhpEnvironment', 'lib/Zend/Http'); 

// Register with spl_autoload:
$loader->register();

$a = new Zend\Http\PhpEnvironment\Request();
print_r($a->getQuery()->get()); exit;

回答by Marcin ?urek

If You have no access to plugin for instance outside of controller You can get params from servicelocator like this

如果您无法访问例如控制器之外的插件,您可以像这样从 servicelocator 获取参数

//from POST
$foo = $this->serviceLocator->get('request')->getPost('foo'); 
//from GET
$foo = $this->serviceLocator->get('request')->getQuery()->foo;
//from route
$foo = $this->serviceLocator->get('application')->getMvcEvent()->getRouteMatch()->getParam('foo');

回答by Lakshmi Ramakrishnan

All the above methods will work fine if your content-type is "application/-www-form-urlencoded". But if your content-type is "application/json" then you will have to do the following:

如果您的内容类型是“application/-www-form-urlencoded”,上述所有方法都可以正常工作。但是,如果您的内容类型是“application/json”,那么您将必须执行以下操作:

$params = json_decode(file_get_contents('php://input'), true); print_r($params);

$params = json_decode(file_get_contents('php://input'), true); 打印_r($params);

Reason : See #7 in https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make

原因:见https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make 中的#7