0% found this document useful (0 votes)
70 views

19 - PHP MVC Frameworks Rest API

REST (Representational State Transfer) is a software architecture style for building scalable web services. Resources in a REST API are uniquely identified by URIs and support standard CRUD operations that map to HTTP methods. A REST client like Postman can be used to test APIs by sending requests with different HTTP methods to the appropriate URIs and receive JSON or XML responses. Symfony controllers can be configured for specific HTTP methods to build RESTful endpoints that perform create, read, update and delete operations on resources.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

19 - PHP MVC Frameworks Rest API

REST (Representational State Transfer) is a software architecture style for building scalable web services. Resources in a REST API are uniquely identified by URIs and support standard CRUD operations that map to HTTP methods. A REST client like Postman can be used to test APIs by sending requests with different HTTP methods to the appropriate URIs and receive JSON or XML responses. Symfony controllers can be configured for specific HTTP methods to build RESTful endpoints that perform create, read, update and delete operations on resources.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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

You might also like