I have a controller which does some file management related to entities. I want this controller reusable:
#[ApiResource(
itemOperations: [
'deleteApiFile' => [
'method' => 'DELETE',
'path' => '/foo/{id}/delete-file',
'read' => false,
'controller' => ApiFileDeleteController::class,
],
],
)]
class Foo { /* ... */ }
#[ApiResource(
itemOperations: [
'deleteApiFile' => [
'method' => 'DELETE',
'path' => '/bar/{id}/delete-file',
'read' => false,
'controller' => ApiFileDeleteController::class,
],
],
)]
class Bar { /* ... */ }
And this is the controller:
class ApiFileDeleteController
{
public function __invoke(string $id): void
{
// I have the $id now, how do I determine which entity it belongs to?
//
// Logic in this controller is always the same and always for an entity
$example = $this->em->getRepository($class)->find($id);
// How do I get $class?
}
}
Question: How can I get the ApiPlatform config in the controller (or __construct
)?
- I could make a
FooApiFileDeleteController
and aBarApiFileDeleteController
, but in the future if I want to expand I need to keep adding controller, just for the classname. I'd radther have a little effort now and in the future dont need to worry about this. - I could add a parameter and add a default value, but then it might be affected by a user, I do not want that.
Edit: Currently checking out processors, based of this similar questionApiPlatform v2.6 doesnt support that unfortunately
I have a controller which does some file management related to entities. I want this controller reusable:
#[ApiResource(
itemOperations: [
'deleteApiFile' => [
'method' => 'DELETE',
'path' => '/foo/{id}/delete-file',
'read' => false,
'controller' => ApiFileDeleteController::class,
],
],
)]
class Foo { /* ... */ }
#[ApiResource(
itemOperations: [
'deleteApiFile' => [
'method' => 'DELETE',
'path' => '/bar/{id}/delete-file',
'read' => false,
'controller' => ApiFileDeleteController::class,
],
],
)]
class Bar { /* ... */ }
And this is the controller:
class ApiFileDeleteController
{
public function __invoke(string $id): void
{
// I have the $id now, how do I determine which entity it belongs to?
//
// Logic in this controller is always the same and always for an entity
$example = $this->em->getRepository($class)->find($id);
// How do I get $class?
}
}
Question: How can I get the ApiPlatform config in the controller (or __construct
)?
- I could make a
FooApiFileDeleteController
and aBarApiFileDeleteController
, but in the future if I want to expand I need to keep adding controller, just for the classname. I'd radther have a little effort now and in the future dont need to worry about this. - I could add a parameter and add a default value, but then it might be affected by a user, I do not want that.
Edit: Currently checking out processors, based of this similar questionApiPlatform v2.6 doesnt support that unfortunately
1 Answer
Reset to default 0In my case I could get away with using a DataPersister (or in ApiPlatform 2.7+) called Processor). Not 100% what I was looking for*, but for now it will do.
class ApiFileUploaderDeleteDataPersister implements ContextAwareDataPersisterInterface
{
public function supports($data, array $context = []): bool
{
return is_a($context['resource_class'], ApiFileUploadInterface::class, true);
}
public function persist($data, array $context = [])
{
return $data;
}
public function remove($data, array $context = [])
{
$this->removeFile($context);
}
}
- Its important to set
read: true
in your entity (by omitting it). Controller isnt needed either:#[ApiResource( itemOperations: [ 'deleteApiFile' => [ 'method' => 'DELETE', 'path' => '/bar/{id}/delete-file', // See: ApiFileUploaderDeleteDataPersister ], ], )] class Bar { /* ... */ }
* The reason this isnt what I was looking for is because this is now a 'hidden' file. A controller gets defined in the ApiPlatform config and is therefor easy to find. This is more a background step. Not the worst though.