gpt4 book ai didi

php - Apigility 子路由未获取父参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:40 26 4
gpt4 key购买 nike

我正在尝试为 Apigility 服务编写一个子路由,在呈现 _self 链接之前,一切路由都很好。

GET http://host/api/service/parameter/gui/page

{
"status": 500,
"title": "Unexpected error",
"describedBy": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"detail": "Missing parameter \"parameter\"",
"details": {
"code": 0,
"message": "Missing parameter \"parameter\"",
"trace": "#0 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(313): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, true, true, Array)\n#1 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Segment.php(409): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, false, true, Array)\n#2 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/Part.php(197): Zend\\Mvc\\Router\\Http\\Segment->assemble(Array, Array)\n#3 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php(351): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#4 /vagrant/vendor/zendframework/zendframework/library/Zend/View/Helper/Url.php(100): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#5 [internal function]: Zend\\View\\Helper\\Url->__invoke('parent.rest.s...', Array, Array, true)\n#6 /vagrant/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(610): call_user_func(Object(Zend\\View\\Helper\\Url), 'parent.rest.s...', Array, Array, true)\n#7 /vagrant/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(634): ZF\\Hal\\Plugin\\Hal->fromLink(Object(ZF\\Hal\\Link\\Link))\n#8 /vagrant/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(667): ZF\\Hal\\Plugin\\Hal->fromLinkCollection(Object(ZF\\Hal\\Link\\LinkCollection))\n#9 /vagrant/vendor/zfcampus/zf-hal/src/Plugin/Hal.php(525): ZF\\Hal\\Plugin\\Hal->fromResource(Object(ZF\\Hal\\Entity))\n#10 /vagrant/vendor/zfcampus/zf-hal/src/View/HalJsonRenderer.php(117): ZF\\Hal\\Plugin\\Hal->renderEntity(Object(ZF\\Hal\\Entity))\n#11 /vagrant/vendor/zendframework/zendframework/library/Zend/View/View.php(205): ZF\\Hal\\View\\HalJsonRenderer->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#12 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(102): Zend\\View\\View->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#13 [internal function]: Zend\\Mvc\\View\\Http\\DefaultRenderingStrategy->render(Object(Zend\\Mvc\\MvcEvent))\n#14 /vagrant/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\\Mvc\\MvcEvent))\n#15 /vagrant/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\\EventManager\\EventManager->triggerListeners('render', Object(Zend\\Mvc\\MvcEvent), Array)\n#16 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(352): Zend\\EventManager\\EventManager->trigger('render', Object(Zend\\Mvc\\MvcEvent))\n#17 /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(327): Zend\\Mvc\\Application->completeRequest(Object(Zend\\Mvc\\MvcEvent))\n#18 /vagrant/public/index.php(38): Zend\\Mvc\\Application->run()\n#19 {main}"
}
}

查看 ZF2 MVC 文档,我有点意识到我需要为我的参数设置一个 metadata_map 条目……但是没有 Apigility 的示例。我读过硬编码没有用,即便如此,这个 parameter 是动态的,目前还不清楚如何从 module.config.php

正确调用>
'zf-hal' => array(
'metadata_map' => array(
'Parent\\V1\\Rest\\Service\\ServiceEntity' => array(
'entity_identifier_name' => 'parameter',
'route_name' => 'parent.rest.service',
'route_identifier_name' => 'parameter',
'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
),
'Parent\\V1\\Rest\\Service\\ServiceCollection' => array(
'entity_identifier_name' => 'parameter',
'route_name' => 'parent.rest.service',
'route_identifier_name' => 'parameter',
'is_collection' => true,
),
'Parent\\V1\\Rest\\Gui\\GuiEntity' => array(
'entity_identifier_name' => 'page',
'route_name' => 'parent.rest.service/gui',
'route_identifier_name' => 'gui_page',
'route_params' => array('parameter'), #<------- unknown setting area, needs to get the parent route identifier somehow...
'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
),
'Parent\\V1\\Rest\\Gui\\GuiCollection' => array(
'entity_identifier_name' => 'page',
'route_name' => 'parent.rest.service/gui',
'route_identifier_name' => 'gui_page',
'route_params' => array('parameter'),
'is_collection' => true,
),
),
),

编辑:添加路由器配置

 'router' => array(
'routes' => array(
'parent.rest.service' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/service[/:parameter]',
'defaults' => array(
'controller' => 'Parent\\V1\\Rest\\Service\\Controller',
),
),
'may_terminate' => true,
'child_routes' => array(
'gui' => array(
'type' => 'Segment',
'options' => array(
'route' => '/gui[/:page]',
'defaults' => array(
'controller' => 'Parent\\V1\\Rest\\Gui\\Controller',
),
),
)
),
),
),
),

最佳答案

我对 Apigility 有一些经验,但对您的配置没有经验。据我所知,这确实意味着 Hal 插件无法为您的名为 parameterroute_identifier 找到一个值,因此它无法呈现自链接。

在您的某些 metadata_map 配置中 (nr 1+2) 似乎应该从资源中获取值:有一条路线,路线名称:

'route_name' => 'parent.rest.service',

为了正确呈现这条路线,我们需要一个名为参数的路线标识符:

'route_identifier_name' => 'parameter',

要获取此路由标识符的值,我们需要实体中名为 parameter 的属性:

'entity_identifier_name' => 'parameter',

很可能实体应该在一个数组中包含数据,并且该数组应该包含一个名为“parameter”的键,然后它会尝试获取与该键对应的值。


在另一个 metadata_map 配置中,您设置了一个“静态”路由参数数组,如下所示:

'route_params' => array('parameter'),

这是不完整的,因为您始终使用键和值设置路由参数数组。所以这应该是这样的:

'route_params' => array('parameter' => 'my_value'),

其中 my_value 可以是字符串、整数等。

希望本文能对您有所帮助。我似乎完全看不出还有什么地方可能出错。

关于php - Apigility 子路由未获取父参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24500865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com