Hi,
I don't use api-platform with Doctrine ORM and i would like to personnalize the POST action. I defined a CollectionProvider to catch the data, but the POST process doesn't enter in action getCollection. Where's possible to personnalize this ?
My use case is: I get the POST data and transfer that to my RabbitMQ. I don't save any data in database.
Thank's for your help.
Bertrand
It's possible to have this ?
Thank's for your help
Bertrand
<?php
namespace ProviderBundle\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ProviderBundle\Entity\Patron;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* PatronCollectionProvider
*/
class PatronCollectionProvider implements CollectionDataProviderInterface
{
/**
* @var null|Request
*/
private $request;
/**
* PatronCollectionProvider constructor.
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
/**
* Retrieves a collection.
*
* @param string $resourceClass
* @param string|null $operationName
*
* @throws ResourceClassNotSupportedException
*
* @return array|PaginatorInterface|\Traversable
*/
public function getCollection(string $resourceClass, string $operationName = null)
{
if (Patron::class !== $resourceClass) {
throw new ResourceClassNotSupportedException();
}
if (Request::METHOD_POST === $operationName) {
// TODO: Catch Patron Object and transform to put into the RabbitMQ
}
return [];
}
}
Did you register it ?
I don't think something like this will work, because it is used to retrieve the data, not to create it.
Did you look at the event part in the documentation, you can do something like we've done with the book listener exemple, but instead of sending an email, you put the object into rabbitmq.
Hi @Simperfit,
I tried to put my code in DataCollection, because i read this in documentation:
https://github.com/api-platform/docs/blob/master/core/operations.md
Collection operations: GET and POST (Create an object) <-- It's not clear
Item operations: GET, PUT and DELETE
I'll watch the events.
Thanks for your answer.
Hi @Simperfit,
I defined my proof of concept with that:
<?php
namespace ProviderBundle\EventSubscriber;
use ProviderBundle\Entity\Patron;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* PatronSubscriber
*/
final class PatronSubscriber implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['sendToRegister', 0]
]
];
}
public function sendToRegister(GetResponseForControllerResultEvent $event)
{
$patron = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$patron instanceof Patron || Request::METHOD_POST !== $method) {
return;
}
echo "SEND TO RABBITMQ"; exit;
}
}
loaded with:
services:
# ---- EVENT SUBSCRIBER
app.patron.subscriber:
class: ProviderBundle\EventSubscriber\PatronSubscriber
tags: [ { name: 'kernel.event_subscriber'} ]
but, my event is not called. I don't find the reason.
Any suggestions ?
Thanks
@Simperfit,
Hello,
I found the problem. If i make 0 on priority (Ex: example in events documentation, see above), the event isn't called. I changed the value to 32 and the event is called.
good week
Bertrand
Most helpful comment
@Simperfit,
Hello,
I found the problem. If i make 0 on priority (Ex: example in events documentation, see above), the event isn't called. I changed the value to 32 and the event is called.
good week
Bertrand