ApiPlatform\Core\EventListener\ReadListener::onKernelRequest PHP Method

onKernelRequest() public method

Calls the data provider and sets the data attribute.
public onKernelRequest ( GetResponseEvent $event )
$event Symfony\Component\HttpKernel\Event\GetResponseEvent
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        try {
            $attributes = RequestAttributesExtractor::extractAttributes($request);
        } catch (RuntimeException $e) {
            return;
        }
        if (isset($attributes['collection_operation_name'])) {
            $data = $this->getCollectionData($request, $attributes);
        } else {
            $data = $this->getItemData($request, $attributes);
        }
        $request->attributes->set('data', $data);
    }

Usage Example

Example #1
0
 /**
  * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 public function testRetrieveItemNotFound()
 {
     $collectionDataProvider = $this->prophesize(CollectionDataProviderInterface::class);
     $itemDataProvider = $this->prophesize(ItemDataProviderInterface::class);
     $itemDataProvider->getItem('Foo', 22, 'get')->willReturn(null)->shouldBeCalled();
     $request = new Request([], [], ['id' => 22, '_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'json', '_api_mime_type' => 'application/json']);
     $request->setMethod(Request::METHOD_GET);
     $event = $this->prophesize(GetResponseEvent::class);
     $event->getRequest()->willReturn($request)->shouldBeCalled();
     $listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal());
     $listener->onKernelRequest($event->reveal());
 }