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

PHPでURLルーティングを自作する

bmf_san
June 29, 2019

 PHPでURLルーティングを自作する

bmf_san

June 29, 2019
Tweet

More Decks by bmf_san

Other Decks in Programming

Transcript

  1. PHPͰURLϧʔςΟϯάΛࣗ࡞͢Δ
    @bmf_san
    @PHP Conference Fukuoka 2019
    2019/06/29

    View full-size slide

  2. @bmf_san
    bmf-san
    bmf-tech.com
    Twitter
    Github
    Blog

    View full-size slide

  3. ֓ཁ
    1. ಈػ
    • ϧʔςΟϯάΛࣗ࡞͠Α͏ͱࢥ͖͔͚ͬͨͬ
    2. Ͳ͏࡞͔ͬͨʁ
    • ࣮૷աఔʹ͍ͭͯ
    3. ·ͱΊ

    View full-size slide

  4. ಈػ
    • ݩʑGolangͰ࣮૷͍ͨ͠ͱߟ͍͑ͯͨ
    • ͱΓ͋͑ͣ׳Ε͍ͯΔPHP͔Βɾɾ
    • ͳΜ͔ࣗ࡞ͯ͠ΈͨΒ໘നͦ͏ʢখฒײʣ

    View full-size slide

  5. 2
    Ͳ͏࡞͔ͬͨʁ

    View full-size slide

  6. ࣮ࡍʹ࡞ͬͨ΋ͷ

    View full-size slide

  7. ιʔε
    • Github
    • bmf-san/ahi-router
    • https://github.com/bmf-san/ahi-router

    View full-size slide

  8. ϧʔςΟϯάΛఆٛ͢Δ
    ୳ࡧ݁ՌΛಘΔ
    ࠓճ࣮૷ͨ͠3PVUFSύοέʔδ

    View full-size slide

  9. ΞΫγϣϯͱύεύϥϝʔλ͕औಘͰ͖Δ

    View full-size slide

  10. ύοέʔδެ։
    • Packagist
    • bmf-san/ahi-router
    • https://packagist.org/packages/bmf-san/ahi-
    router#2.0.0

    View full-size slide

  11. ࣮૷աఔͷ࿩
    • URLϧʔςΟϯάͱ͸Կ͔ʢཁ݅ఆٛʣ
    • ࢓༷ΛܾΊΔ
    • ઃܭ
    • σʔλߏ଄Λߟ͑Δ
    • ࣮૷

    View full-size slide

  12. URLϧʔςΟϯάͱ͸Կ͔

    View full-size slide

  13. URLϧʔςΟϯάͱ͸
    • ϦΫΤετ͞ΕͨURLʹରͯ͠ɺ࣮ߦ͍ͨ͠ॲཧΛฦ͢
    • ύεʢ/foo/bar/1ʣ෦෼Λύʔεͯ͠ɺ೚ҙͷ஋Λฦ͢
    • ਖ਼نදݱ
    • จࣈྻ୳ࡧΞϧΰϦζϜ

    View full-size slide

  14. URLϧʔςΟϯάͱ͸
    IUUQFYBNQMFDPNGPPCBS
    GPPCBS
    JE͕

    BDUJPO͸
    #BS$POUSPMMFS!TIPX
    3PVUFS
    ϧʔςΟϯάॲཧΛߦ͏
    *OQVU
    0VUQVU

    View full-size slide

  15. ࢓༷ΛܾΊΔ

    View full-size slide

  16. ࢓༷
    • ෳ਺ͷύεύϥϝʔλʔΛؚΉURLʹରԠ
    • /foo/bar/:foo/:bar
    • ΞΫγϣϯͱύϥϝʔλʔ৘ใΛฦ͢
    • ϜζΧγΠίτϋϠϥφΠ
    • ଟػೳ
    • ϋΠύϑΥʔϚϯε

    View full-size slide

  17. σʔλߏ଄Λߟ͑Δ

    View full-size slide

  18. σʔλߏ଄Λߟ͑Δ
    IUUQFYBNQMFDPNGPPCBS
    GPPCBS
    JE͕

    BDUJPO͸
    #BS$POUSPMMFS!TIPX
    3PVUFS
    *OQVU
    0VUQVU

    View full-size slide

  19. I/O
    Input
    • Path
    • /foo/bar/1
    • HTTP Method
    • GET/POST…
    Output
    • Action
    • ॲཧ
    • ex. PostController@getPosts
    • Parameters
    • ύεύϥϝʔλʔ
    • ex. /boo/bar/:id

    View full-size slide

  20. Routing Map
    *OQVU 0VUQVU
    3PVUFS
    1BUI
    )551.FUIPE
    "DUJPO
    1BSBNFUFST
    3PVUJOH.BQ
    ϧʔλʔ͕಺෦Ͱ࣋ͭ
    σʔλߏ଄

    View full-size slide

  21. Routing Mapͱ͸
    • Routing Map
    • ϧʔςΟϯάͷͨΊͷσʔλߏ଄
    • Router͕಺෦తʹ࣋ͭ
    • ϧʔςΟϯάͷఆ͔ٛΒੜ੒͞ΕΔ
    • URLͱ࣮ߦ͍ͨ͠ॲཧΛϚοϐϯά

    View full-size slide

  22. Routing
    1BUI .FUIPE "DUJPO
    (&5 *OEFY$POUSPMMFS!JOEFY
    QPTUT (&5 1PTU$POUSPMMFS!HFU1PTUT
    QPTUTJE (&5 1PTU$POUSPMMFS!FEJU
    QPTUTJE 1045 1PTU$POUSPMMFS!VQEBUF
    QPTUTJEUPLFO (&5 1PTU$POUSPMMFS!QSFWJFX
    QPTUTDBUFHPSZ (&5 1PTU$POUSPMMFS!HFU1PTUT#Z$BUFHPSZ
    QSPpMF (&5 1SPpMF$POUSPMMFS!HFU1SPpMF

    View full-size slide

  23. Routing
    1BUI .FUIPE "DUJPO
    (&5 *OEFY$POUSPMMFS!JOEFY
    QPTUT (&5 1PTU$POUSPMMFS!HFU1PTUT
    QPTUTJE (&5 1PTU$POUSPMMFS!FEJU
    QPTUTJE 1045 1PTU$POUSPMMFS!VQEBUF
    QPTUTJEUPLFO (&5 1PTU$POUSPMMFS!QSFWJFX
    QPTUTDBUFHPSZ (&5 1PTU$POUSPMMFS!HFU1PTUT#Z$BUFHPSZ
    QSPpMF (&5 1SPpMF$POUSPMMFS!HFU1SPpMF

    View full-size slide

  24. ໦ߏ଄
    ɹࠜʢSPPUʣ
    ɹࢬʢFEHFʣ
    ɹઅʢOPEFʣ
    ɹ༿ʢMFBGʣ

    View full-size slide

  25. σʔλߏ଄
    1BUI .FUIPE "DUJPO
    (&5 *OEFY$POUSPMMFS!JOEFY
    QPTUT (&5 1PTU$POUSPMMFS!HFU1PTUT
    QPTUTJE (&5 1PTU$POUSPMMFS!FEJU
    QPTUTJE 1045 1PTU$POUSPMMFS!VQEBUF
    QPTUTJEUPLFO (&5 1PTU$POUSPMMFS!QSFWJFX
    QPTUTDBUFHPSZ (&5 1PTU$POUSPMMFS!HFU1PTUT#Z$BUFHPSZ
    QSPpMF (&5 1SPpMF$POUSPMMFS!HFU1SPpMF

    View full-size slide

  26. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5 QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    ˞&/%@10*/5͸ޠฐ͕͋Γͦ͏ʜ

    View full-size slide

  27. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5 QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    (&5QPTUT

    View full-size slide

  28. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*
    /5
    QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    (&5QPTUT

    View full-size slide

  29. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*
    /5
    QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    (&5QPTUT

    View full-size slide

  30. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*
    /5
    QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    (&5QPTUT

    View full-size slide

  31. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*
    /5
    QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5
    (&5QPTUT

    (&51PTU$POUSPMMFS!HFU1PTUT
    10451PTU$POUSPMMFS!FEJU
    )551ϝιουʹԠͯ͡ॲཧΛৼΓ෼͚
    ˞QBSBNFUFST΋Ұॹʹฦ͢

    View full-size slide

  32. UPLFO
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5
    &/%@10*/5 QSPpMF

    DBUFHPSZ
    &/%@10*/5
    QPTUT
    JE
    &/%@10*/5

    View full-size slide

  33. Routerͷ࣮૷
    • ͲΜͳॲཧ͕ඞཁ͔ʁ
    • Routing MapΛੜ੒͢Δ
    • Routing Map͔Β୳ࡧ͢Δ

    View full-size slide

  34. ϧʔςΟϯάϚοϓΛੜ੒
    • ϧʔςΟϯάϚοϓΛੜ੒͢Δ
    • PathɺHTTP MethodɺActionͷσʔληοτ͔Βੜ੒
    • /posts GET PostController@getPosts

    View full-size slide

  35. ϧʔςΟϯάΛఆٛ͢Δ


    View full-size slide

  36. ࢀরʢʣͰ͕Μ͹Δ

    View full-size slide

  37. ϧʔςΟϯάϚοϓ͔Β୳ࡧ
    • ϧʔςΟϯάϚοϓ͔Β୳ࡧ͢Δ
    • ଟ࣍ݩ഑ྻΛ୳ࡧ͢Δॲཧ
    • →໦ߏ଄ͷ୳ࡧΞϧΰϦζϜ

    View full-size slide

  38. ՝୊
    • ΤϥʔϋϯυϦϯά
    • ࣮ӡ༻ʹ޲͚ͨػೳ։ൃ
    • ϛυϧ΢ΣΞͷͭͳ͗ࠐΈ
    • ύεύϥϝʔλʹਖ਼نදݱ͕࢖͑Δ
    • ͦͷଞศརػೳͱ͔
    • ࣮ߦ଎౓ͷߟྀ
    • ໦ߏ଄ɺจࣈྻ୳ࡧΞϧΰϦζϜ

    View full-size slide

  39. ·ͱΊ
    • ָ͍͠
    • ࠶ؼॲཧ΍ࢀরʢ&ʣΛ࢖͏ྑ͍࿅शʹͳͬͨ
    • ΞϧΰϦζϜΛֶͿ͖͔͚ͬʹͳͬͨ
    • ΑΓྑ͍ઃܭɺύϑΥʔϚϯεΛ໨ࢦ͢

    View full-size slide

  40. ιʔε
    • Github
    • bmf-san/bmf-router
    • https://github.com/bmf-san/bmf-router

    View full-size slide