Вы можете использовать систему событий для этого.
В этом примере я предполагаю, что вы использовали сущность с именем Order
/**
* @ApiResource
*/
class Order {
...
}
<?php
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['isOrderCancelled', EventPriorities::PRE_VALIDATE],
],
];
}
public function isOrderCancelled(ViewEvent $event)
{
// first check if this affects the requested resource
$resource = $event->getRequest()->attributes->get('_api_resource_class');
if (Order::class !== $resource) {
return;
}
// second check if this is the put_item controller
$controller = $event->getRequest()->attributes->get('_controller');
if ('api_platform.action.put_item' !== $controller) {
return;
}
// third fetch the object instance
$order = $event->getControllerResult();
// you can throw an exception if you order is already cancelled. Or something along those lines
}
}