@@ -95,11 +95,9 @@ This class needs to implement the `SolidWorx\SimpleResponseBundle\ResponseHandle
95
95
96
96
``` yml
97
97
services :
98
- some.service.name :
99
- class : My\Custom\Handler
98
+ My\Custom\Handler :
100
99
arguments : ['@doctrine.orm.entity_manager']
101
- tags :
102
- - { name: 'response_handler.handler' }
100
+ tags : ['solidworx.response_handler']
103
101
104
102
` ` `
105
103
@@ -108,16 +106,18 @@ You then need to create a class that will be used as the return value in your ac
108
106
109
107
` ` ` php
110
108
<?php
109
+ use Symfony\Component\HttpFoundation\JsonResponse;
111
110
112
- class DoctrineEntityResponse
111
+ class DoctrineEntityResponse extends JsonResponse
113
112
{
114
113
private $entity;
115
114
116
115
public function __construct(string $entity)
117
116
{
118
117
$this->entity = $entity;
118
+ parent::__construct();
119
119
}
120
-
120
+
121
121
public function getEntity() : string
122
122
{
123
123
return $this->entity;
@@ -129,6 +129,8 @@ Your handler class will add the logic to return a response object;
129
129
130
130
``` php
131
131
<?php
132
+ use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
133
+ use Symfony\Component\HttpFoundation\Response;
132
134
133
135
class Handler implements ResponseHandlerInterface
134
136
{
@@ -139,14 +141,14 @@ class Handler implements ResponseHandlerInterface
139
141
$this->em = $entityManager;
140
142
}
141
143
142
- public function supports($object): bool
144
+ public function supports(Response $object): bool
143
145
{
144
- return $object instance of DoctrineEntityReponse; // Only support responses of this type
146
+ return $object instanceof DoctrineEntityReponse; // Only support responses of this type
145
147
}
146
148
147
- public function handle($object)
149
+ public function handle(Response $object): Response
148
150
{
149
- return new JsonResponse ($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
151
+ return $object->setData ($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
150
152
}
151
153
}
152
154
```
@@ -162,7 +164,7 @@ class MyAction
162
164
{
163
165
public function __invoke()
164
166
{
165
- return new DoctrineEntityResponse('AppBundle: Order' ); // Pass the Order entity which will return all orders in a JSON response
167
+ return new DoctrineEntityResponse(\App\Entity\ Order::class ); // Pass the Order entity which will return all orders in a JSON response
166
168
}
167
169
}
168
170
0 commit comments