2 回答
TA贡献1802条经验 获得超5个赞
路由器很难通过任一侧的变量来确定。在第一个/像something/{tenant}/customers. 但是,错误的原因很可能是根据您的路由列表到达路由器的第一个 GET 路径是:
{tenant}/customers/{customer}
因为这是第一个,Laravel预计会有一个客户变量进来。如果将此行放在更高的位置,则不会每次都期望变量。所以:
{tenant}/customers/{customer}
{tenant}/customers/
这应该会有所帮助......但它可能不是由于两边的通配符 - 你必须测试。
如果您将这些设置为 a resource,我建议您将它们分解为单独的路由方法进行测试
TA贡献1803条经验 获得超6个赞
终于在三天后,我找到了来源,异常消息误导了我。
/**
* Get links to fetch the model or one of its relationships.
*
* @param string|null $relationshipName
* @return array
*/
public function getApiLinks(string $relationshipName = null)
{
$urlGenerator = app()->make('url');
$identifiers = $this->getApiIdentifiers();
if ($relationshipName) {
return [
'self' => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'index'),
'related' => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'related')
];
}
return [
'self' => $urlGenerator->route(
'json-api.'.$identifiers['type'].'.show',
[ Str::singular($identifiers['type']) => $identifiers['id'] ]
];
问题来自返回时的 URL 生成,任何额外的 URL 占位符都不会包含在数组中,顺便说一下导致此消息。
有了这个修复,它现在可以工作了:
/**
* Get links to fetch the model or one of its relationships.
*
* @param string|null $relationshipName
* @return array
*/
public function getApiLinks(string $relationshipName = null)
{
$urlGenerator = app()->make('url');
$identifiers = $this->getApiIdentifiers();
$otherParams = $urlGenerator->getRequest()->route()->parameters();
if ($relationshipName) {
return [
'self' => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'index', $otherParams),
'related' => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'related', $otherParams)
];
}
return [
'self' => $urlGenerator->route(
'json-api.'.$identifiers['type'].'.show',
array_merge(
$otherParams,
[ Str::singular($identifiers['type']) => $identifiers['id'] ]
)
)
];
}
无论如何感谢您的帮助!
- 2 回答
- 0 关注
- 98 浏览
添加回答
举报