-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
39 changed files
with
656 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# RESTful Webservices in Symfony | ||
|
||
## Coding Challenge 6 - POST vs. PUT | ||
|
||
### Tasks | ||
|
||
- implement controllers to create and update attendees and workshops | ||
- both should be possible with JSON and XML | ||
|
||
### Solution | ||
|
||
- use an `ArgumentValueResolver` to deserialize the request's content | ||
- use the `EntityManager` to save the object into the database | ||
- *CREATE:* use HTTP method `POST`, return an HTTP 201 (Created) status code and | ||
set the `Location` header with the help of the `UrlGenerator` | ||
- *UPDATE:* use HTTP method `PUT`, return an HTTP 204 (No Content) status code and leave the response body empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EventListener; | ||
|
||
use App\Negotiation\ContentNegotiator; | ||
use App\Negotiation\MimeType; | ||
use App\Negotiation\RequestFormat; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
#[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest', priority: 8)] | ||
final class RequestFormatListener | ||
{ | ||
private array $formats = [ | ||
RequestFormat::JSON => MimeType::JSON, | ||
RequestFormat::JSON_HAL => MimeType::JSON_HAL, | ||
RequestFormat::XML => MimeType::XML, | ||
]; | ||
|
||
public function __construct( | ||
private ContentNegotiator $contentNegotiator | ||
) { | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
$this->addRequestFormats($request, $this->formats); | ||
|
||
$request->setRequestFormat( | ||
$this->contentNegotiator->getNegotiatedRequestFormat() | ||
); | ||
} | ||
|
||
/** | ||
* Adds the supported formats to the request. | ||
* | ||
* This is necessary for {@see Request::getMimeType} to work. | ||
*/ | ||
private function addRequestFormats(Request $request, array $formats): void | ||
{ | ||
foreach ($formats as $format => $mimeType) { | ||
$request->setFormat($format, $mimeType); | ||
} | ||
} | ||
} |
Oops, something went wrong.