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

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

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

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

YAMAOKA Hiroyuki

January 26, 2019
Tweet

More Decks by YAMAOKA Hiroyuki

Other Decks in Technology

Transcript

  1. 1)1ͷNJEEMFXBSFΛ
    ࢖͍͜ͳͨ͢Ίʹ
    1)1ΧϯϑΝϨϯεઋ୆

    ߹ಉձࣾςϯϚυɹࢁԬ޿޾

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. ͓࿩Λ࢝ΊΔલʹ

    View full-size slide

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

    View full-size slide

  7. Ͱ͸ɺ͋ΒͨΊͯ

    View full-size slide

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

    View full-size slide

  9. ؆୯ʹඳ͘ͱ͜͏

    View full-size slide

  10. XFCΞϓϦͷલॲཧ
    શϦΫΤετڞ௨Ͱ͍ͨ͠ॲཧ
    ϩάΠϯೝূ
    %#͔ΒͷσʔλऔಘɹͳͲ

    ΞϓϦέʔγϣϯຊମͷલʹ

    ॲཧΛڬΊΔΑ͏ʹ͢Ε͹Α͍ͷͰ͸

    View full-size slide

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

    View full-size slide

  12. XFCΞϓϦͷޙॲཧ
    શϦΫΤετڞ௨ͷॲཧɺ·ͩ͋Δ
    ϩάग़ྗ
    ॲཧޙʹ·ͱΊͯϝʔϧૹ৴

    ΞϓϦέʔγϣϯຊମͷޙʹ΋

    ॲཧΛڬΊΔΑ͏ʹͰ͖ΔͱΑͦ͞͏

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. ೖΕࢠߏ଄
    ೖΕࢠࣜʹॲཧΛ

    ͭͳ͍Ͱ͍͘࢓૊ΈΛߟ͑Δ
    ྫؔ਺"ͱؔ਺#Λ࣮૷͢Δ
    ؔ਺"͸ͦͷॲཧͷதͰؔ਺#Λ

    ࣮ߦɺͦͷ݁ՌΛฦ͢Α͏ʹ͢Δ

    View full-size slide

  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

    View full-size slide

  17. ೖΕࢠߏ଄

    View full-size slide

  18. ͞ΒʹೖΕࢠߏ଄

    View full-size slide

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

    IUUQTXXXqJDLSDPNQIPUPTEBSXJOCFMM

    View full-size slide

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

    View full-size slide

  21. XFCΞϓϦʹద༻

    View full-size slide

  22. 1)1ʹ͓͚Δྺ࢙

    View full-size slide

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

    View full-size slide

  24. 143ͱ͸ʁ
    IUUQTXXXQIQpHPSHQTS
    l1)14UBOEBSET3FDPNNFOEBUJPOTz
    ܾఆతͳʮඪ४ʯͰ͸ͳ͍
    ͋͘·ͰΦεεϝɺקࠂ
    ίϛϡχςΟʔͰ͸Өڹྗେ

    View full-size slide

  25. Α͘ฉ͘143
    143#BTJD$PEJOH4UBOEBSE
    143$PEJOH4UZMF(VJEF
    143-PHHFS*OUFSGBDF
    143"VUPMPBEJOH4UBOEBSE

    View full-size slide

  26. 143
    IUUQTXXXQIQpHPSHQTSQTS
    )551NFTTBHFJOUFSGBDFT
    1TSa)UUQa.FTTBHFa3FRVFTU*OUFSGBDF
    1TSa)UUQa.FTTBHFa3FTQPOTF*OUFSGBDF
    XFCΞϓϦͷೖग़ྗͷඪ४Խ

    View full-size slide

  27. 143
    IUUQTXXXQIQpHPSHQTSQTS
    )5514FSWFS3FRVFTU)BOEMFST
    1TSa)UUQa4FSWFSa3FRVFTU)BOEMFS*OUFSGBDF
    1TSa)UUQa4FSWFSa.JEEMFXBSF*OUFSGBDF
    XFCΞϓϦͷඪ४Խ

    View full-size slide

  28. ઌ΄Ͳͷਤ

    View full-size slide

  29. .JEEMFXBSF*OUFSGBDF
    interface MiddlewareInterface
    {
    public function process(
    ServerRequestInterface $request,
    RequestHandlerInterface $handler
    ): ResponseInterface;
    }

    View full-size slide

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

    View full-size slide

  31. ࠷ۙͷ143ͷ࿩୊
    IUUQTUXJUUFSDPNGBCQPUTUBUVT

    3FNPWF4ZNGPOZCZGBCQPUu1VMM
    3FRVFTUuQIQpHpHTUBOEBSET

    View full-size slide

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

    View full-size slide

  33. 4MJN'SBNFXPSLW
    IUUQTXXXTMJNGSBNFXPSLDPN
    )5513PVUFS
    .JEEMFXBSF
    1434VQQPSU
    %FQFOEFODZ*OKFDUJPO 143

    View full-size slide

  34. 4MJN

    class ExampleMiddleware
    {
    public function __invoke(
    ServerRequestInterface $request,
    ResponseInterface $response,
    callable $next
    ): ResponseInterface {
    return $next($request, $response);
    }
    }

    View full-size slide

  35. 4MJN'SBNFXPSLW
    IUUQTHJUIVCDPNTMJNQIQ4MJN
    CMPCY$)"/(&-0(NE
    4MJNOPMPOHFSTIJQTXJUIB143
    JNQMFNFOUBUJPO
    1433FRVFTU)BOEMFSTDBOOPX
    CFVTFEBTSPVUFDBMMBCMFT

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  38. -BSBWFM

    class ExampleMiddleware
    {
    public function handle(
    \Illuminate\Http\Request $request,
    \Closure $next
    ) {
    return $next($request);
    }
    }

    View full-size slide

  39. 4ZNGPOZ
    NJEEMFXBSFʹ૬౰͢Δػೳ͸ͳ͍
    ػೳͱͯ͠͸ϑΟϧλʔ͕͋Δ
    IUUQTTZNGPOZDPNEPDDVSSFOU
    FWFOU@EJTQBUDIFS
    CFGPSF@BGUFS@pMUFSTIUNM

    View full-size slide

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

    View full-size slide

  41. $BLF1)1

    class ExampleMiddleware
    {
    public function handle(
    ServerRequestInterface $request,
    ResponseInterface $response,
    callable $next
    ): \Cake\Http\Response {
    return $next($request, $response);
    }
    }

    View full-size slide

  42. ͯ͞ɺ·ͱΊ

    View full-size slide

  43. ͪΐͬͱେ͛͞ͳ࿩

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. NJEEMFXBSF
    ͍͔͕Ͱ͔ͨ͠ʁ

    View full-size slide

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

    View full-size slide

  51. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide