Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Zend Framework 2 + API

Zend Framework 2 + API

ZFDay in Kiev, 2011

Victor Farazdagi

October 24, 2011
Tweet

More Decks by Victor Farazdagi

Other Decks in Programming

Transcript

  1. !"#$%& '#()*+(,*- Richardson Maturity Level (http://goo.gl/aYlT) The REST Architectural Style

    (http://goo.gl/DxTPB) Separating REST Facts from Fallacies (http://goo.gl/Xik2A) Roy Fielding’s Dissertation (http://goo.gl/Q71Y) Haters gonna HATEOS (http://goo.gl/1lrLu) Vendor Specific Mime Types - RFC4288 (http://goo.gl/o0fu6)
  2. REST Refresher REST - *&-./$0/(&+4? 2/.#3 B/" /*0"$ REST? Resources

    vs Representations (R & R) Richardson Maturity Model (RMM)
  3. ."*)1)')3#) REST Client/Server C/2(/2/6.$ 0"+/$02/* (Stateless) D'.+4? E+/$&A$?2 (Uniform Interface)

    F$G.&($)"2/3 (Caching) H+"9"(&"6+$6*, I.2/$)* (Layered System) F"' %" ;*%&"2( (Code on Demand)
  4. 2'+3 $%61+3#7 Web API ."*)1)'#(4$7 $ R & R (Resources

    and their Representations) <)(+'# #$"%'46%0+3#7 HTTP "*%(%&%'+ Security HTTP !"#$%& '()*($#+* $,-($+ .$%& /$/#$01-0 '2)*1-3"1-0 4*5)$/$6 .",-)$6*1-" 7$3+- 68$%*
  5. <%$(,"3-) ;)(%1- GET HEAD PUT POST DELETE PATCH 9$:;3"1-" 5)"%/#*6:"1-0

    )"/;)/*. <%"1#-3"1 GET, ("4 +$1#"1#*. =*!"1* )"/;)/*. >$(*6:"1-" (5$%-))"/;)/*. ?%*:"1-" )"/;)/*. <4!"1"1-" )"/;)/*.
  6. >%1- $%$(%73#7 !"#$% On Success On Failure POST GET PATCH

    DELETE PUT 201  Created Location: 400  Bad  Request 200  OK 400  Bad  Request 404  Not  Found 200  OK 400  Bad  Request 404  Not  Found 204  No  Content 400  Bad  Request 404  Not  Found 410  Gone 204  No  Content 400  Bad  Request 404  Not  Found
  7. 2*%:#) 1)(+'# ?%:&# 0@%1+ >)A#*%0+3#) ./*+/%(&+ %A#/%& .B*+3#:)3#) &%'#:)$(0+ 6+"*%$%0

    >%1#*%0&+ $#;0%'%0 Cross Origin Resource Sharing JSON-P Pagination
  8. C)*$#%33%$(4 API L*:$) +(=+* 6$&2."++"2/3? 1"@*6#$+.$ 6$&2.. 6 URI https://api.my.com/v3/customers/123

    1"@*6#$+.$ %*&*)$/&* https://api.my.com/customers/123?v=3 E2%"#3;"6*+.$ HTTP-;*9"#"60"6 ?
  9. C)*$#7 0 URI @A%/*(*3+* :( B*:*%"/ :) https://api.my.com/v3.0/customer/123 ====> GET

     /v3.0/customer/123  HTTP/1.1 Accept:  application/json <==== HTTP/1.1  200  OK Content-­‐Type:  application/json {    "customer":  {          "version":  "0.3",          "name":  "Sasha  Beliy"    } } https://api.my.com/customer/123 ====> GET  /customer/123  HTTP/1.1 Accept:  application/vic.myapp-­‐v3+json <==== HTTP/1.1  200  OK Content-­‐Type:  application/vic.myapp-­‐v3+json {    "customer":  {          "name":  "Sasha  Beliy"    } } https://api.my.com/customer/123?format=xml ====> GET  /customer/123?format=xml  HTTP/1.1 https://api.my.com/customer/123 ====> GET  /customer/123  HTTP/1.1 Accept:  application/xml
  10. User Entity class  User {        //  skip

           /**          *  @ORM\OneToMany(targetEntity="Gist",          *                                mappedBy="user")          */        protected  $gists;        //  skip }
  11. Gist Entity class  Gist {        //  skip

           /**          *  @ORM\ManyToOne(targetEntity="User",                                            inversedBy="gists")          */        protected  $user;                //  skip }
  12. F3()*G)9$ List Get a single gist Create a gist Edit

    a gist Star/Unstar a gist Is a gist starred? Delete a gist GET  /gists GET  /gists/starred GET  /gists/:id POST  /gists PATCH  /gists/:id PUT  /gists/:id/star DELETE  /gists/:id/star GET  /gists/:id/star DELETE  /gists/:id
  13. 2*)1$(+0')3#) &$'()# *'+("' JSON 1  { 2      

       "id":  42, 3          "description":  "some  description", 4          "content":  "some  content" 5  }
  14. Setup Controller Action /**  *  GET  /gists/:id  */ public  function

     get($id) {        $gist  =  $this-­‐>getService()                                  -­‐>get($id);        $this-­‐>response-­‐>setStatusCode(200);        $repr  =  new  JsonRepresentation($gist);        return  array(                'content'  =>  $repr-­‐>toString()  );         }
  15. Entity Service public  function  get($id) {        $gist

     =  $this-­‐>em                                  -­‐>find('Gists\Entity\Gist',  $id);        if  ($gist)  {                $repr  =  new  GistIteratorAggregate($gist);        }        throw  NotFoundException('Not  Found'); }
  16. Setup Routing return  array(        'routes'  =>  array(

                   'gists'  =>  array(                        'type'        =>  'Zend\Mvc\Router\Http\Regex',                        'options'  =>  array(                        'regex'      =>                                            '/gists(/(?<id>[0-­‐9]*[^/]+)?)?',                                'defaults'  =>  array(                                        'controller'  =>  'gists_index',                                ),                        ),                ),        ), );
  17. Unit Testing >(% 3) "%3#;+)( 6+:);, (%B% "%613% $"+$+(4! Zend\Mvc\ModuleManager

    GistsTests\Framework\TestCase ?)$(#*,);: !)*0#$3-9 $'%9 H%1)'#
  18. UTs Bootstrap use  GistsTest\Framework\TestCase; $moduleManager  =  new  \Zend\Module\Manager(array(    'SpiffyDoctrine',

     'Application',  'Gists') ); $moduleManager-­‐>loadModules(); $mergedConfig  =  $moduleManager    -­‐>getMergedConfig()    -­‐>toArray(); //  sets  up  (and  exposes):   //  service  locator,  entity  manager  etc TestCase::$config  =  $mergedConfig;
  19. Test Case Setup namespace  GistsTest; class  FooTest  extends  Framework\TestCase {

           protected  $service;        public  function  setUp()        {                parent::setup();        //  manners                $this-­‐>service  =  $this-­‐>getLocator()                                                            -­‐>get('api');        }
  20. Functional Testing /**  *  GET  /gists/:id/star  */ public  function  testIsGistStaredTrue()

    {        $result  =  $this-­‐>getCurl()-­‐>request(                'GET',  $gist-­‐>value  .  '/star'        );        $this-­‐>assertSame(                'HTTP/1.1  204  No  Content',                    $result['status']        ); }
  21. Load Testing F3$(*,;)3(- ab siege php + curl + loop

    K)'4: L63+(4 (%:&, $'%;+, "%37(4 (roughly!) 3+ :(% $"%$%/)3 $)*0#$ 0 ()&,M)9 &%3G#B,*+J##
  22. N 0- 63+)(), :(% .. O#634 %:)34 ,"*%M+)($7 )$'# ,

    0+$ )$(4: H%6B# 2%3#;+3#) B'+0)3$(0,5M)9 "+*+1#B;- H%1,'43-) # (!) G,3&J#%3+'43-) ()$(- 8)"*)*-03+7 #3()B*+J#7