PHPのmiddlewareを 使いこなすために

PHPのmiddlewareを 使いこなすために

PHPカンファレンス仙台2019での発表資料です。
https://phpcon-sendai.net/2019/

325afd2c9db540c83485f509f2845acb?s=128

Hiroyuki Yamaoka

January 26, 2019
Tweet

Transcript

  1. 1)1ͷNJEEMFXBSFΛ ࢖͍͜ͳͨ͢Ίʹ 1)1ΧϯϑΝϨϯεઋ୆ 
 ߹ಉձࣾςϯϚυɹࢁԬ޿޾

  2. ࣗݾ঺հ  ࢁԬ޿޾!IJSP@Z  1)1ͱ͔/PEFKTͷΤϯδχΞ  1)1͸ͷ͜Ζ͔Β  ߹ಉձࣾςϯϚυ୅දࣾһ

  3. ɹɹ߹ಉձࣾςϯϚυ!!  Ϗδωεࢧԉࣄۀ  ࣾ֎$50ͱ͔ΞυόΠβʔ  *OOPWBUPS+BQBO64&/.FEJBͳͲ  αʔϏε։ൃࣄۀ 

    JSVDBNJNFNPDPOBTV
  4. ࠓճͷ͓࿩ͷΰʔϧ  NJEEMFXBSFͱ͍͏ߟ͑ํ͕Θ͔Δ  ฉ͍ͨͱ͖ʹΠϝʔδ͕ු͔Ϳ  ར༻γʔϯ͕૝૾Ͱ͖Δ  ϑϨʔϜϫʔΫͰ࢖͑Δ͔Θ͔Δ

  5. ͓࿩Λ࢝ΊΔલʹ

  6. લճ·Ͱͷ͋Β͢͡  1)1ΧϯϑΝϨϯεؔ੢  4MJN'SBNFXPSLͰ࢝ΊΔ1)1ͷ NJEEMFXBSF  1)1ΧϯϑΝϨϯε  1)1'*(ͷ)551ॲཧඪ४ͷઃܭ

    ͸ͳͥ143ʹͳͬͨͷ͔
  7. Ͱ͸ɺ͋ΒͨΊͯ

  8. XFCΞϓϦͱ͸  ϦΫΤετΛड͚औΓʢೖྗʣ  ͍Ζ͍ΖॲཧΛͯ͠  ೝূͱ͔%#ͷॲཧͱ͔  ϨεϙϯεΛฦ͢ʢग़ྗʣ

  9. ؆୯ʹඳ͘ͱ͜͏

  10. XFCΞϓϦͷલॲཧ  શϦΫΤετڞ௨Ͱ͍ͨ͠ॲཧ  ϩάΠϯೝূ  %#͔ΒͷσʔλऔಘɹͳͲ  ΞϓϦέʔγϣϯຊମͷલʹ
 ॲཧΛڬΊΔΑ͏ʹ͢Ε͹Α͍ͷͰ͸

  11. ϑΟϧλʔʢલॲཧʣ

  12. XFCΞϓϦͷޙॲཧ  શϦΫΤετڞ௨ͷॲཧɺ·ͩ͋Δ  ϩάग़ྗ  ॲཧޙʹ·ͱΊͯϝʔϧૹ৴  ΞϓϦέʔγϣϯຊମͷޙʹ΋
 ॲཧΛڬΊΔΑ͏ʹͰ͖ΔͱΑͦ͞͏

  13. ϑΟϧλʔʢޙॲཧʣ

  14. ͯ͞ɺͪΐͬͱ ൃ૝Λస׵͢Δ࿩

  15. ೖΕࢠߏ଄  ೖΕࢠࣜʹॲཧΛ
 ͭͳ͍Ͱ͍͘࢓૊ΈΛߟ͑Δ  ྫؔ਺"ͱؔ਺#Λ࣮૷͢Δ  ؔ਺"͸ͦͷॲཧͷதͰؔ਺#Λ
 ࣮ߦɺͦͷ݁ՌΛฦ͢Α͏ʹ͢Δ

  16. 1)1Ͱॻ͍ͯΈΔͱ $a = function (string $str, \Closure $next) : string

    { echo 'A'; // લॲཧΆ͍
 $result = $next($str); // ࣍ΛݺͿ echo 'C'; // ޙॲཧΆ͍ return $result; };
 $b = function (string $str) : string { echo 'B';
 return strtoupper($str); };
 $a('hoge', $b); // ABC
  17. ೖΕࢠߏ଄

  18. ͞ΒʹೖΕࢠߏ଄

  19. λϚωΪΈ͍ͨʁ QIPUPCZEBSXJO#FMMMJDFOTFEVOEFS$$#:  
 IUUQTXXXqJDLSDPNQIPUPTEBSXJOCFMM

  20. ೖΕࢠߏ଄ͷಛ௃  ΞϓϦຊମΛแΉΑ͏ʹ࣮૷  ֎ଆ͔Β৽͍͠;Δ·͍Λ௥ՃͰ͖Δ  ࢀߟ"01ʢΞεϖΫτࢦ޲ϓϩάϥϛϯάʣ  ڞ௨ͷॲཧΛԣஅతʹݺͼग़͠ 

    લॲཧɾޙॲཧΛࢦఆͰ͖Δ
  21. XFCΞϓϦʹద༻

  22. 1)1ʹ͓͚Δྺ࢙

  23. 4UBDL1)1  IUUQTTUBDLQIQDPN  l$PNQPTJOH)UUQ,FSOFM*OUFSGBDF NJEEMFXBSFTTJODFz  l8IBUEPXFXBOU 3FVTFBUUIF XFCMBZFSz

     4ZNGPOZͷ)UUQ,FSOFM*OUFSGBDF
  24. 4UBDL1)1

  25. 143ͱ͸ʁ  IUUQTXXXQIQpHPSHQTS  l1)14UBOEBSET3FDPNNFOEBUJPOTz  ܾఆతͳʮඪ४ʯͰ͸ͳ͍  ͋͘·ͰΦεεϝɺקࠂ 

    ίϛϡχςΟʔͰ͸Өڹྗେ
  26. Α͘ฉ͘143  143#BTJD$PEJOH4UBOEBSE  143$PEJOH4UZMF(VJEF  143-PHHFS*OUFSGBDF  143"VUPMPBEJOH4UBOEBSE

  27. 143  IUUQTXXXQIQpHPSHQTSQTS  )551NFTTBHFJOUFSGBDFT  1TSa)UUQa.FTTBHFa3FRVFTU*OUFSGBDF  1TSa)UUQa.FTTBHFa3FTQPOTF*OUFSGBDF 

    XFCΞϓϦͷೖग़ྗͷඪ४Խ
  28. 143  IUUQTXXXQIQpHPSHQTSQTS  )5514FSWFS3FRVFTU)BOEMFST  1TSa)UUQa4FSWFSa3FRVFTU)BOEMFS*OUFSGBDF  1TSa)UUQa4FSWFSa.JEEMFXBSF*OUFSGBDF 

    XFCΞϓϦͷඪ४Խ
  29. ઌ΄Ͳͷਤ

  30. 143143

  31. .JEEMFXBSF*OUFSGBDF interface MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface

    $handler ): ResponseInterface; }
  32. 3FRVFTU)BOEMFS interface RequestHandlerInterface { public function handle( ServerRequestInterface $request ):

    ResponseInterface; }
  33. ࠷ۙͷ143ͷ࿩୊  IUUQTUXJUUFSDPNGBCQPUTUBUVT   3FNPWF4ZNGPOZCZGBCQPUu1VMM 3FRVFTUuQIQpHpHTUBOEBSET

  34. ϑϨʔϜϫʔΫͱ NJEEMXBSFͷؔ܎

  35. 4MJN'SBNFXPSLW  IUUQTXXXTMJNGSBNFXPSLDPN  )5513PVUFS  .JEEMFXBSF  1434VQQPSU 

    %FQFOEFODZ*OKFDUJPO 143
  36. 4MJN  class ExampleMiddleware { public function __invoke( ServerRequestInterface $request,

    ResponseInterface $response, callable $next ): ResponseInterface { return $next($request, $response); } }
  37. 4MJN'SBNFXPSLW  IUUQTHJUIVCDPNTMJNQIQ4MJN CMPCY$)"/(&-0(NE  4MJNOPMPOHFSTIJQTXJUIB143 JNQMFNFOUBUJPO  1433FRVFTU)BOEMFSTDBOOPX CFVTFEBTSPVUFDBMMBCMFT

  38. ;FOE&YQSFTTJWF  IUUQTEPDT[FOEGSBNFXPSLDPN [FOEFYQSFTTJWF  1431433PVUJOH143  ඪ४Λҙࣝ

  39. -BSBWFM  IUUQTMBSBWFMDPNEPDT NJEEMFXBSF  143ͷ࣮૷Ͱ͸ͳ͍  ैདྷͷDPOUSPMMFSͷิॿతͳ໾ׂ  ڞ௨ॲཧΛ࣮૷͢ΔͨΊͷ࢓૊Έ

  40. -BSBWFM  class ExampleMiddleware { public function handle( \Illuminate\Http\Request $request,

    \Closure $next ) { return $next($request); } }
  41. 4ZNGPOZ  NJEEMFXBSFʹ૬౰͢Δػೳ͸ͳ͍  ػೳͱͯ͠͸ϑΟϧλʔ͕͋Δ  IUUQTTZNGPOZDPNEPDDVSSFOU FWFOU@EJTQBUDIFS CFGPSF@BGUFS@pMUFSTIUNM

  42. $BLF1)1  IUUQTCPPLDBLFQIQPSHFO DPOUSPMMFSTNJEEMFXBSFIUNM  ͔Β143ʹରԠ  $43'τʔΫϯνΣοΫͷ࣮૷ͳͲ͕ NJEEMFXBSFͰͳ͞Ε͍ͯΔ

  43. $BLF1)1  class ExampleMiddleware { public function handle( ServerRequestInterface $request,

    ResponseInterface $response, callable $next ): \Cake\Http\Response { return $next($request, $response); } }
  44. ͯ͞ɺ·ͱΊ

  45. ͪΐͬͱେ͛͞ͳ࿩

  46. ͦ΋ͦ΋ϑϨʔϜϫʔΫͱ͸  ϦΫΤετΛड͚औΔ  SPVUJOHΛݩʹߦ͏ॲཧΛબ୒͢Δ  ॲཧΛݺͼग़͠ɺ࣮ߦ  ݁ՌΛϨεϙϯεͱͯ͠ग़ྗ͢Δ

  47. ϑϨʔϜϫʔΫ ࡞Εͦ͏͡Όͳ͍Ͱ͔͢ʁ

  48. ϑϨʔϜϫʔΫϨγϐ  143ͷ࣮૷ʢ[FOEEJBDUPSPTͳͲʣ  SPVUJOH࣮૷ʢ'BTU3PVUFͳͲʣ  NJEEMFXBSFͷ࣮ߦʢ.JEEMFMBOEͳͲʣ  ग़ྗʢ[FOEIUUQIBOEMFSSVOOFSͳͲʣ

  49. ࣮ྫ  IUUQTMPHNJKQUFDIBSUJDMFT  ϩάϛʔͷϦχϡʔΞϧ  lࠓճͷϦχϡʔΞϧͰ͸͜ͷ143 Λར༻͢Δ͜ͱͰɺಛఆͷ8FCϑϨʔ ϜϫʔΫΛར༻ͤͣʹΞϓϦΛ࣮૷͢ Δ͜ͱʹ͠·ͨ͠z

  50. ΦϨΦϨϑϨʔϜϫʔΫ  ϑϨʔϜϫʔΫ؆୯ʹ࡞ΕΔ  143ʹ४ڌ͓͚ͯ͠͹ɺޙ͔Β;FOE &YQSFTTJWFͳͲʹ৐Γ׵͑Δ͜ͱ΋Մೳ  ΋ͪΖΜɺٯ΋  બ୒ࢶͷҰͭͱͯ͠ɺ͋Γ͔΋

  51. NJEEMFXBSF ͍͔͕Ͱ͔ͨ͠ʁ

  52. ·ͱΊ  ॲཧΛNJEEMFXBSFͰ෼ׂ  هड़͕؆ܿʹɺςετ༰қʹ  143Ͱඪ४Խ͞ΕΔ͜ͱͰࠩ͠ସ͑Մೳʹ  %*ίϯςφͱͷ૊Έ߹Θͤ΋ศར 

    ίϯετϥΫλΠϯδΣΫγϣϯ
  53. ͋Γ͕ͱ͏͍͟͝·ͨ͠