Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Create custom Collection URI (uriTemplate) #2777

Unanswered
GaylordP asked this question in Q&A
Discussion options

Hello and thank you for your wonderful library. :)

With API Platform 3, I have a PHP entity called Report.
I would like to offer two ways to retrieve a collection:

/reports: the standard URI generated by API Platform.
/admin/reports: an additional URI with a specific normalizationContext.

However, when I implement this code, I can only get one collection instead of both.

enter image description here

<?php

...

#[ORM\Entity(repositoryClass: ReportRepository::class)]
#[ApiResource(
    uriTemplate: '/admin/reports{._format}',
    operations: [
        new GetCollection(security: "is_granted('ROLE_ADMIN')"),
        new Get(security: "is_granted('ROLE_ADMIN')"),
        new Patch(security: "is_granted('ROLE_ADMIN')"),
    ],
    normalizationContext: [
        AbstractNormalizer::GROUPS => ['Report:read:admin'],
    ],
    denormalizationContext: [
        AbstractNormalizer::GROUPS => ['Report:write:admin'],
    ],
    order: ['createdAt' => 'DESC']
)]
#[ApiResource(
    operations: [
        new GetCollection(),
        new Post(),
        new Get(
            security: 'object.getCreatedBy() == user',
            controller: NotFoundAction::class, 
            read: false, 
            output: false
        )
    ],
    normalizationContext: [
        AbstractNormalizer::GROUPS => ['Report:read'],
    ],
    denormalizationContext: [
        AbstractNormalizer::GROUPS => ['Report:write'],
    ],
    order: ['createdAt' => 'DESC']
)]
class Report
{
    ...

I was expecting to have 6 routes and not 5, as stated in the entity (including two collections).
Does anyone have an idea? Thanks a lot! :)

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.