REST API with Symphony
Table of Contents
• RESTful Web Services
– Representational State Transfer (REST)
– CRUD Operations and HTTP Methods
– Postman – REST Client
2
RESTFUL WEB SERVICES
Lightweight Architecture for Web Services
What is REST?
"Representational State Transfer (REST) is a
software architecture style consisting of
guidelines and best practices for creating
scalable Web services."http://en.wikipedia.org/wiki/Representational_State_Transfer
• Application state and functionality are resources
– Every resource is associated with unique URI
– Each resource supports standard operations (CRUD)
• This natively maps to the HTTP protocol
– HTTP methods: GET, POST, PUT, DELETE, PATCH, OPTIONS, …
REST & HTTP
• Request methods:
– GET - Get resource
– POST - Create resource
– PUT - Update resource
– PATCH - Partly update resource
– DELETE - Delete resource
– HEAD - Get headers for resource
• Response: json, xml
5
CRUD Operations in REST APIs
6
RESTful Web Services and HTTP Methods
• One URI per resource
– Multiple operations per URI
• Get all resources / single resource by ID
– GET http://myservice.com/api/Books
– GET http://myservice.com/api/Books/3
• Add a new resource
– POST http://myservice.com/api/Books
• Modify (update) a resource
– PUT http://myservice.com/api/Books/3
7
RESTful Web Services and HTTP Methods
(2)
• Delete (remove) a resource
– DELETE http://myservice.com/api/Books/3
• Update a resource partially
– PATCH http://myservice.com/api/Books/3
• Retrieve resource meta-data
– HEAD http://myservice.com/api/Books/3
• Inspect resource (typically used in AJAX to request
permissions)
– OPTIONS http://myservice.com/api/Books/3
8
Postman
Postman – REST Client
10
REST Controllers
• A Symfony controller configured for specific http method
11
REST Controllers
• Add prefix to your controller using the @Route annotation
12
REST Controllers
• GET method
13
REST Controllers
• GET method
14
REST Controllers
• GET by Id method
15
REST Controllers
• GET by Id method
16
REST Controllers
• POST method
17
REST Controllers
• POST method
18
REST Controllers
• DELETE method
19
REST Controllers
• DELETE method
20
REST Controllers
• PUT method
21
REST Controllers
• PUT method
22
RESTful API – Example
Server
Auth
Register
Web Client (JavaScript and jQuery)
Login
$.post("api/register", credentials, 'json');
Operations
$.post("api/login", credentials, 'json');
Users
$.getJSON("api/users");
Remove User
23
JSON
• JSON (JavaScript Object Notation)
– Standard for representing data structures and associative
arrays
– Lightweight text-based open standard
– Derived from the JavaScript language
{
"firstName": "John", "lastName": "Smith", "age": 25,
"address": { "streetAddress": "17 Tintyava Str.",
"city": "Sofia", "postalCode": "1113" },
"phoneNumber": [{ "type": "home", "number": "212 555-1234"},
{ "type": "fax", "number": "646 555-4567" }]
},
{ "firstName": "Bay", "lastName": "Ivan", "age": 79 }
24
Summary
§ REST
§ Rest concepts
§ Rest URIs
§ Responses
§ CRUD Operations and HTTP Methods
25