Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure route identifiers for subresources #1918

Closed
lucascourot opened this issue May 2, 2018 · 9 comments
Closed

Configure route identifiers for subresources #1918

lucascourot opened this issue May 2, 2018 · 9 comments

Comments

@lucascourot
Copy link

Hi,

is it possible to customize the route identifier names in ApiPlatform when using subresources?

Example:
I want my route
/api/customers/{id}/entity_types/{entityTypes}/layouts
to become
/api/customers/{customerId}/entity_types/{entityTypeId}/layouts
so that I can easily implement a custom SubresourceDataProviderInterface.

I quickly tried to figure out where the magic was done and found out this method

$operation['identifiers'] = [['id', $rootResourceClass, true]];

For me it looks like it's not configurable but I wanted to ask to be sure.

Thanks.

@soyuka
Copy link
Member

soyuka commented May 2, 2018

Oh you just want to customize parameters names. I looked into this and it's not supported (yet?). Indeed, they're generated in the SubresourceOperationFactory (and the names have a special meaning to find associations later in the SubresourceOperationDataProvider). I doubt that this would be an interesting feature though.

About your data provider you should be able to create one without changing those identifiers names, they will be:

<?php

[
  'id' => 1, // customerId, or the root of the subresource
  'entityTypes' => 2 // entity type id
]

One thing that may work is to override the whole path of the subresource operation (see custom operation in the docs) and add your own data provider.

@lucascourot
Copy link
Author

About your data provider you should be able to create one without changing those identifiers names

That's what I do, the problem is, APIP generates several routes and the parameter names conflict with each other.

Example:
First generated route
/api/customers/{id}/entity_types/{entityTypes}/layouts
Here the entity type id is named entityTypes

Second generated route
/api/entity_types/{id}/layouts
Here the entity type id is named just id 😭

One thing that may work is to override the whole path of the subresource operation (see custom operation in the docs)

You mean creating custom controllers https://api-platform.com/docs/core/operations/#creating-custom-operations-and-controllers?

@soyuka
Copy link
Member

soyuka commented May 2, 2018

parameter names conflict with each other.

They don't conflict, I mean for 1 route there are no parameters with the same name :).

First generated route
/api/customers/{id}/entity_types/{entityTypes}/layouts
Here the entity type id is named entityTypes

Second generated route
/api/entity_types/{id}/layouts
Here the entity type id is named just id sob

Yes, the subresource_context is there to help you make a correlation between entity and id.

You should be able to override the subresource operation path:

@ApiResource({subresourceOperations={"find_name_here": {"path": "/my/new/path"}})

@Simperfit Simperfit changed the title Configure route identifiers Configure route identifiers for subresources May 2, 2018
@lucascourot
Copy link
Author

Thanks, subresourceOperations did the job. But now, my Swagger is completely lost.

The route is now correct ( /api/customers/{customerId}/entity_types/{entityTypeId}/layouts) but the parameters are still called id and entityTypes. The "try it out" feature doesn't work either.

@soyuka
Copy link
Member

soyuka commented May 2, 2018

@lucascourot
Copy link
Author

yes but your links don't mention the possibility to rewrite the parameter names of the default routes (getItem, getSubresource, etc.).
I rewrote all of the them but I now get errors Unable to generate an IRI for the item of type ... because it internally tries to generate routes by passing values for params named "id" etc.

@soyuka
Copy link
Member

soyuka commented May 2, 2018

I guess there is some more work to be done here then :). What would be a potential solution for your issue?

@lucascourot
Copy link
Author

Not sure. I think of two solutions.

First one is by stop hacking around Api Platform and do it from scratch (I already developed a "domain" layer which is framework asgnostic. I wanted to see if I could easily connect my domain model to APIP to delegate it all the REST concerns).

The second one is by stop trying to decouple from a domain layer and do it the APIP way by bringing back DoctrineORM and generate anemic Doctrine entities and using all the magic APIP provides for CRUD & RAD based apps.

@soyuka
Copy link
Member

soyuka commented Apr 7, 2019

related to api-platform/api-platform#1022 merged in #2706

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants