Skip to content

Allow to specify a route prefix per resource #1685

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

Merged
merged 4 commits into from
Feb 24, 2018

Conversation

Toflar
Copy link
Contributor

@Toflar Toflar commented Feb 7, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR api-platform/docs#391

Sometimes it's useful to be able to specify a prefix for all operations of a given resource. In that case right now you have to override the paths of all operations individually.
The goal of this PR is to allow this:

/**
* @ApiResource("attributes"={"routePrefix"="/foobar-prefix"})
 */
class Entity {}

@Toflar Toflar force-pushed the resource-route-prefix branch from eaed80b to 481ccd8 Compare February 7, 2018 17:08
/**
* @var string
*/
public $routePrefix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a new attribute for that? Can't we use an attribute?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking about that, maybe can we just forward any top-level attribute passed to the annotation to attributes? This way it will be easier for the user. Anyway if we create a new attribute, the XML and the YAML loader must be extracted too (it's why I'm 👎).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well to be honest, I have no idea what qualifies for a top-level attribute and what can be part of attributes :-D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas we should totally freeze current top-level stuff and only allow updates with optional attributes IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I found it strange that I need to update all the factories. So I'm moving this to attributes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please, I also think it's better to use attributes.

@soyuka what do you think about the fallback mechanism I was mentioning earlier?

@@ -197,8 +198,11 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
}
}

$path = $resourceMetadata->getRoutePrefix() ?: '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

@Toflar Toflar force-pushed the resource-route-prefix branch from ddf9734 to c042ec7 Compare February 8, 2018 16:27
@Toflar
Copy link
Contributor Author

Toflar commented Feb 8, 2018

Okay, comments addressed. Makes the PR a lot smaller 😄 Will do a docs PR once this is approved.

* @param string $operationType
*
* @throws RuntimeException
* @param RouteCollection $routeCollection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop this block, we now don't add PHPDoc when not adding information compared to the typehints. Just keep the @throw if appropriate.

@Toflar
Copy link
Contributor Author

Toflar commented Feb 9, 2018

Docs PR linked. PR ready.

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@api-platform/core-team please don't merge before the release of 2.2.

@@ -197,8 +193,11 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
}
}

$path = $resourceMetadata->getAttribute('routePrefix', '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about sanitizing the prefix with '/'.trim(trim($prefix, '/')) if it's defined (!== '')?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too but then my idea was that in Symfony, you always have to start with / both, for prefixes as well as routes. And if you define them incorrectly, you see an invalid path so that's a quickfix. Not sure we should do this :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was just to follow the same logic as RouteCollection::addPrefix().
IMO it can improve the UX.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Included 👍

@antograssiot
Copy link
Contributor

Just asking but should this option be applied even if path is defined at operation level ? That would avoid using this option if a single operation doesn't need it, and therefore force the developer to use path again on every operation...

@Toflar
Copy link
Contributor Author

Toflar commented Feb 12, 2018

To me, I think chances are higher that you would want to "namespace" the whole entity. So in fact, also when you use path on an operation, you want to have the prefix applied as well. Your example would mean you want the prefix for 3 operations except for 2 and in cases like these I think it's okay to specify path on every operation.

@antograssiot
Copy link
Contributor

@Toflar I have seen several cases of entites with 10+ operations and only 2 routes with different namespace. If you for instance need several permissions + extensions to be applied for automatic filtering of the same collection based on user "type"
If the attribute is overridable at operation level, that's enough anyway I think

@Toflar
Copy link
Contributor Author

Toflar commented Feb 12, 2018

The problem is, it's separated logic. The OperationPathResolver can return whatever path it wants to. I cannot find out whether the prefix should be applied or not.

@Toflar Toflar force-pushed the resource-route-prefix branch from 24db267 to aa4ecdb Compare February 12, 2018 16:30
@dunglas dunglas merged commit 0f608ff into api-platform:master Feb 24, 2018
@dunglas
Copy link
Member

dunglas commented Feb 24, 2018

Thanks @Toflar

@Toflar Toflar deleted the resource-route-prefix branch April 5, 2018 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants