Webservices werden heute in vielen Bereichen der IT zur Integration unterschiedlicher Anwendungen verwendet. Die Klasse der REST-Webservices spielt dabei eine besondere Rolle, da REST sich auf die Grundlagen von HTTP stützt, einfach verständlich ist und relativ einfach in bestehende Anwendungen zu integrieren ist. Dieser Vortrag gibt einen Überblick über die Herausforderungen einer RESTful API und zeigt, wie diese mit der Hilfe von Symfony einfach gelöst werden können.
Diese Slides habe ich für meinen Vortrag auf der DWX 2014 genutzt.
7. GET /movies
HTTP/1.1 200 OK
Date: Mon, 14 Jul 2014 12:10:00 GMT
{
"movies": [
{
"title": "Indiana Jones and the Temple of Doom",
"uri": "/movies/1"
},
{
"title": "Indiana Jones and the Last Crusade",
"uri": "/movies/2"
},
{
"title": "Indiana Jones and the Temple of the Forbidden Eye",
"uri": "/movies/3"
}
]
}
8. POST /movies
POST /movies HTTP/1.1
Content-Type: application/json
{
"movie": {
"title": "Indiana Jones and the Kingdom of the Crystal Skull",
"releaseDate": "22 May 2008"
}
}
10. GET /movies/1
HTTP/1.1 200 OK
Date: Mon, 14 Jul 2014 12:10:00 GMT
{
"movie": {
"title": "Indiana Jones and the Temple of Doom",
"releaseDate": "22 May 1984"
},
"uri": "/movies/1"
}
11. PUT /movies/1
PUT /movies/1 HTTP/1.1
Content-Type: application/json
{
"movie": {
"title": "Indiana Jones and the Temple of Doom",
"releaseDate": "23 May 1984"
}
}
25. SERIALISIERUNG
$result = new MovieResult(new Movie('Star Wars: A New Hope', '25 May 1977'));
$serializedContent = $serializer->serialize($result, 'json');
echo $serializedContend;
{
"movie": {
"title": "Star Wars: A New Hope",
"releaseDate": "25 May 1977"
}
}
35. DER CONTROLLER
Übersetzt aus HTTP-Logik in Applikations-Logik
Erstellt Responses oder gibt angeforderten Daten zurück
Arbeitet (fast) format-agnostisch
Und bitte mit !DTOs
36. <?php
class MoviesController
{
public function getAction()
{
return $this->movieApiService->getMovieList();
}
public function postAction(Request $request)
{
$movie = $this->serializer->deserialize(
$request->getContent(),
'MovieDto',
$request->attributes->get('_contentType')
);
$this->movieApiService->addMovie($movie);
$response = new Response('', 201);
$response->headers->set(
'Location',
$this->generateUrl('movie_get', ['id' => $movie->getId()])
);
return $response;
}
}
37. VOM CONTROLLER ZUM MODELL
Controller Service
Domain
Model
DTO Mapper
Validator
43. LITERATUR
Martin Fowler -
Richardson Maturity Model
Roy Fielding's Dissertation
"Architectural Styles and the Design of Network-based
Software Architectures"
Patterns of Enterprise Application
Architecture