ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
ReST (Introduction)


 3rd Meetup PHP Indonesia

         6 April 2013
          Surabaya



       by Glend Maatita
ReST example (Twitter)
ReST example (Facebook)
ReST - History

¡ñ   Representational State Transfer


¡ñ   ReST was re-DISCOVER-ed by Roy Fielding


¡ñ   Doesnt require any external library


¡ñ   Tightly bond with HTTP
ReST ¨C HTTP
Client Send Request           Server Send Response




GET / HTTP/1.1
Host: xkcd.com            HTTP/1.1 200 OK
Accept: text/html         Date: Sat, 02 Apr 2011 21:05:05 GMT
User-Agent: Mozilla/5.0   Server: lighttpd/1.4.19
(Macintosh)
                          Content-Type: text/html
                          <html>...</html>
ReST - HTTP
    HTTP Verb
¡ñ   GET
¡ñ   POST
¡ñ   PUT
¡ñ   DELETE


    Routes
¡ñ   URL + HTTP Verb
ReST - Definition


¡°The Same Thing with a different STATE
changes its meaning¡±


¡°The message is changed based on it's
State¡±
ReST ¨C How To
¡ñ   Loved by developer
¡ñ   Easy to read, easy to
    understand, easy to learn
ReST
Bad !!                      Imagine :)


/getAllUsers                /getMentions
/getUserById                /getUserTimeLine
/getUsers                   /getHomeTimeLine
/deleteUser                 /getStatusesRetweet
/updateUser                 /updateStatuses
/deleteUserTenYearsOld      /destroyStatus
/newUser                    /getRetweets
/getUserUnderTenYearsOld
ReST - Quotes


¡°Web it's not about HTML pages, it's about resources,
    and those resources are located by URLs¡±
So ?
¡ñ We only need two base URLs as the location of a resource
(collection of resource, and single resource)
¡ñ Resources is Noun, use Noun :)

¡ñ Plural is better




    Resource    POST          GET (READ)       PUT            DELETE
                (CREATE)                       (UPDATE)


    /users      Create new    List all Users   Update All     Delete All
                user                           Users          Users


    /users/1    -             Show User        Update User Delete User
                              (with ID = 1)    with ID = 1 (if with ID = 1
                                               Exist)
Association ?
Easy :)


GET /departments/1/users
POST /departments/1/users
PUT /users/123
DELETE /users/123
Query
Use '?' for Query


GET /users?limit=50&offset=100
GET /users?username=indra&birth=1990
GET /users?column=id,name,username,birth
GET /users.json (/users.xml)
Versioning
/v1/users/
Terima Kasih




                Glend Maatita
                  @glend_maatita
                  YM : the_entung
      glend.maatita@kampus.co.id

More Related Content

PHP Indonesia 3rd meetup - 06/04/13

  • 1. ReST (Introduction) 3rd Meetup PHP Indonesia 6 April 2013 Surabaya by Glend Maatita
  • 4. ReST - History ¡ñ Representational State Transfer ¡ñ ReST was re-DISCOVER-ed by Roy Fielding ¡ñ Doesnt require any external library ¡ñ Tightly bond with HTTP
  • 5. ReST ¨C HTTP Client Send Request Server Send Response GET / HTTP/1.1 Host: xkcd.com HTTP/1.1 200 OK Accept: text/html Date: Sat, 02 Apr 2011 21:05:05 GMT User-Agent: Mozilla/5.0 Server: lighttpd/1.4.19 (Macintosh) Content-Type: text/html <html>...</html>
  • 6. ReST - HTTP HTTP Verb ¡ñ GET ¡ñ POST ¡ñ PUT ¡ñ DELETE Routes ¡ñ URL + HTTP Verb
  • 7. ReST - Definition ¡°The Same Thing with a different STATE changes its meaning¡± ¡°The message is changed based on it's State¡±
  • 8. ReST ¨C How To ¡ñ Loved by developer ¡ñ Easy to read, easy to understand, easy to learn
  • 9. ReST Bad !! Imagine :) /getAllUsers /getMentions /getUserById /getUserTimeLine /getUsers /getHomeTimeLine /deleteUser /getStatusesRetweet /updateUser /updateStatuses /deleteUserTenYearsOld /destroyStatus /newUser /getRetweets /getUserUnderTenYearsOld
  • 10. ReST - Quotes ¡°Web it's not about HTML pages, it's about resources, and those resources are located by URLs¡±
  • 11. So ? ¡ñ We only need two base URLs as the location of a resource (collection of resource, and single resource) ¡ñ Resources is Noun, use Noun :) ¡ñ Plural is better Resource POST GET (READ) PUT DELETE (CREATE) (UPDATE) /users Create new List all Users Update All Delete All user Users Users /users/1 - Show User Update User Delete User (with ID = 1) with ID = 1 (if with ID = 1 Exist)
  • 12. Association ? Easy :) GET /departments/1/users POST /departments/1/users PUT /users/123 DELETE /users/123
  • 13. Query Use '?' for Query GET /users?limit=50&offset=100 GET /users?username=indra&birth=1990 GET /users?column=id,name,username,birth GET /users.json (/users.xml)
  • 15. Terima Kasih Glend Maatita @glend_maatita YM : the_entung glend.maatita@kampus.co.id