Skip to content

Commit a695b29

Browse files
authored
Documenting rest route (#848)
1 parent 14b9c09 commit a695b29

File tree

1 file changed

+95
-21
lines changed

1 file changed

+95
-21
lines changed

book/extend-admin.rst

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,27 @@ shows a controller doing what has just been described.
194194
namespace App\Controller\Admin;
195195
196196
use App\Entity\Event;
197-
use FOS\RestBundle\Controller\Annotations\RouteResource;
198-
use FOS\RestBundle\Routing\ClassResourceInterface;
199-
use FOS\RestBundle\View\View;
200-
use FOS\RestBundle\View\ViewHandlerInterface;
201197
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface;
202198
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
203199
use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;
204200
use Sulu\Component\Rest\RestHelperInterface;
201+
use Symfony\Component\HttpFoundation\JsonResponse;
205202
use Symfony\Component\HttpFoundation\Response;
203+
use Symfony\Component\Serializer\SerializerInterface;
206204
207-
/**
208-
* @RouteResource("event")
209-
*/
210-
class EventController implements ClassResourceInterface
205+
class EventController
211206
{
212207
public function __construct(
213-
private ViewHandlerInterface $viewHandler,
214208
private FieldDescriptorFactoryInterface $fieldDescriptorFactory,
215209
private DoctrineListBuilderFactoryInterface $listBuilderFactory,
216-
private RestHelperInterface $restHelper
210+
private RestHelperInterface $restHelper,
211+
private SerializerInterface $serializer,
217212
) {
218213
}
219214
215+
/**
216+
* Gets a list of all events ina a paginated response
217+
*/
220218
public function cgetAction(): Response
221219
{
222220
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors(Event::RESOURCE_KEY);
@@ -231,19 +229,95 @@ shows a controller doing what has just been described.
231229
$listBuilder->count()
232230
);
233231
234-
return $this->viewHandler->handle(View::create($listRepresentation));
232+
return new JsonResponse($this->serializer->serialize($listRepresentation->toArray()));
233+
}
234+
235+
/**
236+
* Gets a single event
237+
*/
238+
public function getAction(int $id): Response
239+
{
240+
$event = $this->eventRepository->find($id);
241+
242+
return new JsonResponse($this->serializer->serialize($event));
243+
}
244+
245+
/**
246+
* Creates a new event
247+
*/
248+
public function postAction(Request $request): Response
249+
{
250+
$event = new Event();
251+
$this->mapRequestToEvent($request, $event);
252+
253+
$this->eventRepository->persist($event);
254+
$this->eventRepository->flush();
255+
256+
return new JsonResponse($this->serializer->serialize($event)));
257+
}
258+
259+
/**
260+
* Updates an existing event by finding it, mapping new data onto it and saving it
261+
*/
262+
public function putAction(int $id, Request $request): Response
263+
{
264+
$event = $this->eventRepository->find($id);
265+
266+
$this->mapRequestToEvent($request, $event);
267+
268+
$this->eventRepository->flush();
269+
270+
return new JsonResponse($this->serializer->serialize($event)));
271+
}
272+
273+
public function deleteAction(int $id): Response
274+
{
275+
$event = $this->eventRepository->find($id);
276+
$this->eventRepository->remove($event);
277+
$this->eventRepository->flush();
278+
279+
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
280+
}
281+
282+
/**
283+
* Maps the request body to an event event object
284+
*/
285+
private function mapRequestToEvent(Request $request, Event $event): void
286+
{
287+
$event->setName($request->request->get('name'));
288+
$event->setStartDate($request->request->get('startDate'));
289+
$event->setEndDate($request->request->get('endDate'));
235290
}
236291
}
237292
238293
Register your new Controller in the ``config/routes_admin.yaml`` file the following way:
239294

240295
.. code-block:: yaml
241296
242-
app_events_api:
243-
type: rest
244-
prefix: /admin/api
245-
resource: App\Controller\Admin\EventController
246-
name_prefix: app.
297+
app.get_events:
298+
path: /admin/api/events
299+
controller: App\Controller\Admin\EventController::cgetAction
300+
methods: [GET]
301+
302+
app.get_event:
303+
path: /admin/api/events/{id}
304+
controller: App\Controller\Admin\EventController::getAction
305+
methods: [GET]
306+
307+
app.post_event:
308+
path: /admin/api/events
309+
controller: App\Controller\Admin\EventController::postAction
310+
methods: [POST]
311+
312+
app.put_event:
313+
path: /admin/api/events/{id}
314+
controller: App\Controller\Admin\EventController::putAction
315+
methods: [PUT]
316+
317+
app.delete_event:
318+
path: /admin/api/events/{id}
319+
controller: App\Controller\Admin\EventController::deleteAction
320+
methods: [DELETE]
247321
248322
Configure resources
249323
-------------------
@@ -254,11 +328,11 @@ as well, then you should be able to see these actions when using the ``debug:rou
254328
.. code-block:: bash
255329
256330
$ bin/adminconsole debug:router | grep event
257-
app.get_events GET ANY ANY /admin/api/events.{_format}
258-
app.post_event POST ANY ANY /admin/api/events.{_format}
259-
app.get_event GET ANY ANY /admin/api/events/{id}.{_format}
260-
app.put_event PUT ANY ANY /admin/api/events/{id}.{_format}
261-
app.delete_event DELETE ANY ANY /admin/api/events/{id}.{_format}
331+
app.get_events GET ANY ANY /admin/api/events
332+
app.post_event POST ANY ANY /admin/api/events
333+
app.get_event GET ANY ANY /admin/api/events/{id}
334+
app.put_event PUT ANY ANY /admin/api/events/{id}
335+
app.delete_event DELETE ANY ANY /admin/api/events/{id}
262336
263337
.. note::
264338

0 commit comments

Comments
 (0)