Here are the routes I'm working on (generated by apiResource
Laravel's method). As you can see, there are 1 or 2 placeholders. My issue comes when I try to GET anything/customers
. It raises this exception:
Missing required parameters for [Route: json-api.customers.show] [URI: {tenant}/customers/{customer}].
Unless I'm missing an obvious thing, the router should accept this request because anything/customers
matches {tenant}/customers
.
I'd really appreciate any help about that. Thanks in advance.
EDIT: I add this code to answer a comment but I don't think it will help to understand this issue (I'm implementing a package based on JSON:API spec).
protected function jsonApiResource()
{
return function (string $class, array $options = []) {
if ($routerMethod = $class::getRouterMethod()) {
$middleware = array_merge(
$options['middleware'] ?? [],
$class::getApiMiddlewares()
);
$as = $options['as'] ?? '';
$prefix = $class::getRouterPrefix();
$this->group(compact('middleware', 'as', 'prefix'), function ($router) use ($class, $routerMethod, $options) {
$alias = $class::getAlias();
$controller = $class::getControllerClass();
$router->{$routerMethod}(
$alias,
$controller,
Arr::only($options, ['only', 'except'])
);
foreach ($class::getRelationsRoutes() as $relationshipName => $relationshipMethods) {
$router->resourceRelationship(
$alias,
$relationshipName,
$controller,
$relationshipMethods
);
}
});
}
};
}
Finally after 3 days on that, I found out the origin, the exception message misled me.
/**
* 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'] ]
];
The issue came from the URL generation on the return, any additional URL placeholder wouldn't be included into the array and by the way led to this message.
With this fix, it works now:
/**
* 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'] ]
)
)
];
}
Thanks anyway for your help!