URLルーティングをつくる

0c9a69560f916778b569086d792680bb?s=47 bmf_san
March 31, 2019

 URLルーティングをつくる

0c9a69560f916778b569086d792680bb?s=128

bmf_san

March 31, 2019
Tweet

Transcript

  1. URLϧʔςΟϯάΛͭ͘Δ @bmf_san @PHPerKaigi 2019 2019/03/31

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

  3. ֓ཁ 1. ಈػ 2. ࣗ࡞΁ͷಓͷΓ • લఏɹ • ࣮૷લͷաఔ •

    ࢓༷ͱ͔ઃܭ·ͱΊ 3. ࣮૷ • ۩ମతͳ࣮૷಺༰ʹ͍ͭͯ͸͋·Γ৮Ε·ͤΜɻʢ࣌ؒత౎߹ʣ • ࣮૷͢ΔࡍͷϙΠϯτ 4. ·ͱΊ • ࣗ࡞ͯ͠Έͨॴײ
  4. ιʔε • Github • bmf-san/ahi-router • https://github.com/bmf-san/ahi-router

  5. 1 ಈػ

  6. ಈػ • goͰϧʔςΟϯάΛࣗલͰ࣮૷͔ͨͬͨ͠ • Ͱ΋goͦΜͳʹ׳Ε͍ͯͳ͍… • phperkaigiͷcfpग़͍ͨ͠ɺͰ΋ωλ͕… • ͦ΋ͦ΋ϧʔςΟϯάͬͯͲ͏΍࣮ͬͯ૷͞Ε͍ͯΔΜ ͩΖ͏…

  7. ؐΖ͏͓ΕͨͪͷPHP΁

  8. 2 ࣗ࡞΁ͷಓͷΓ

  9. ͜Μͳͷ͕Ͱ͖ͨ

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

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

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

  13. URLϧʔςΟϯάͱ͸ IUUQFYBNQMFDPNGPPCBS GPPCBS JE͕
 BDUJPO͸ #BS$POUSPMMFS!TIPX 3PVUFS ϧʔςΟϯάॲཧΛߦ͏ *OQVU 0VUQVU

  14. PHPͷϧʔςΟϯάϥΠϒϥϦ • FastRoute • https://github.com/nikic/FastRoute • Pux • https://github.com/c9s/Pux •

    Klein • https://github.com/klein/klein.php • etc…
  15. ࢓༷ • ෳ਺ͷύεύϥϝʔλʔΛؚΉURLʹରԠ • /foo/bar/:foo/:bar • ΞΫγϣϯͱύϥϝʔλʔ৘ใΛฦ͢ • ϜζΧγΠίτϋϠϥφΠ •

    ศརͳػೳͱ͔
  16. RouterΛͭ͘Δ IUUQFYBNQMFDPNGPPCBS GPPCBS JE͕
 BDUJPO͸ #BS$POUSPMMFS!TIPX 3PVUFS *OQVU 0VUQVU

  17. I/O Input • Path • /foo/bar/1 • HTTP Method •

    GET/POST… • Routing Definition • ϧʔςΟϯάͷఆٛ Output • Action • ॲཧʢฦ͍ͨ͠஋ʣ • ex. PostController@getPosts • Parameters • ύεύϥϝʔλʔ • ex. /boo/bar/1 • /foo/bar/:id • → id 1
  18. Routing Map *OQVU 0VUQVU 3PVUFS 1BUI )551.FUIPE 3PVUJOH%FpOJUJPO 1BUI )551.FUIPE

    3PVUJOH.BQ ϧʔςΟϯάͷͨΊ ͷσʔλߏ଄
  19. Routing Mapͱ͸ • Routing Map • ϧʔςΟϯάͷͨΊͷσʔλߏ଄ • Router͕಺෦తʹ࣋ͭ •

    URLͱ࣮ߦ͍ͨ͠ॲཧΛϚοϐϯά
  20. Routing Map *OQVU 0VUQVU 3PVUFS 3PVUJOH.BQ ϧʔςΟϯάͷͨΊ ͷσʔλߏ଄ .BQΛੜ੒ .BQ͔Β୳ࡧ

  21. Routing 1BUI .FUIPE "DUJPO  (&5 *OEFY$POUSPMMFS!JOEFY QPTUT (&5 1PTU$POUSPMMFS!HFU1PTUT

    QPTUTJE (&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 &/%@10*/5 &/%@10*/5 &/%@10* /5 QSPpMF  DBUFHPSZ &/%@10*/5

    QPTUT JE &/%@10*/5 (&5QPTUT 
  28. UPLFO &/%@10*/5 &/%@10*/5 &/%@10*/5 &/%@10* /5 QSPpMF  DBUFHPSZ &/%@10*/5

    QPTUT JE &/%@10*/5 (&5QPTUT 
  29. UPLFO &/%@10*/5 &/%@10*/5 &/%@10*/5 &/%@10* /5 QSPpMF  DBUFHPSZ &/%@10*/5

    QPTUT JE &/%@10*/5 (&5QPTUT 
  30. 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΋Ұॹʹฦ͢
  31. UPLFO &/%@10*/5 &/%@10*/5 &/%@10*/5 &/%@10*/5 QSPpMF  DBUFHPSZ &/%@10*/5 QPTUT

    JE &/%@10*/5
  32. ࣮૷

  33. 3 ࣮૷

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

  35. ࣮૷Πϝʔδ *OQVU 0VUQVU 3PVUFS 3PVUJOH.BQ ϧʔςΟϯάͷͨΊ ͷσʔλߏ଄ .BQΛੜ੒ .BQ͔Β୳ࡧ

  36. ͓໿ଋ • PHPͷਖ਼نදݱ͸ۃྗආ͚Δ • ΦϨΦϨΞϧΰϦζϜͰߟ͑ͯΈ͔ͨͬͨ • ͱΓ͋͑ͣಈ͘Ϟϊʹ͸͢Δ • ϓϩμΫγϣϯͰ࢖͏Ϩϕϧ·Ͱ͸ͪΐͬͱ…

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

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

  39. None
  40. ϧʔςΟϯάΛఆٛ͢Δ  

  41. ࣮૷

  42. ࢀরʢʣͰ͕Μ͹Δ

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

  44. None
  45. 1)1ͷਖ਼نදݱ͸ආ͚ͯΦϨΦϨจࣈྻ୳ࡧ

  46. ՝୊ • ͪΌΜͱ΍Ε͍ͯͳ͍ • ΤϥʔϋϯυϦϯά • ࣮ߦ଎౓ͷߟྀ • ໦ߏ଄ɺจࣈྻ୳ࡧΞϧΰϦζϜΛֶͿ •

    Ͱ͖ͨΒྑͦ͞͏ • ωʔϜϧʔτʢϧʔςΟϯάͷάϧʔϐϯάʣ • ύεύϥϝʔλʔʹਖ਼نදݱΛ࢖͑ΔΑ͏ʹ͢Δ • :id → \d ਺஋͚ͩώοτ͢ΔΑ͏ʹ͢Δɺͱ͔ • ϛυϧ΢ΣΞͱͷͭͳ͗ࠐΈ
  47. ύοέʔδެ։ • Packagist • bmf-san/ahi-router • https://packagist.org/packages/bmf-san/ahi- router#2.0.0

  48. 4 ·ͱΊ

  49. ·ͱΊ • 158ߦͰಈ͍ͨ • https://github.com/bmf-san/ahi-router/blob/master/src/ Router.phpɹ • ࠶ؼॲཧͱύϫʔϓϨΠͰͳΜͱ͔͍͚Δ • ͪΌΜͱ΍ΔͳΒ

    • ໦ߏ଄ͷΞϧΰϦζϜʢτϥΠ໦ɺج਺໦ɺύτϦγΞ ໦… etcʣΛֶΜͰదͨ͠΋ͷΛબఆ͢Δ
  50. ͓·͚ • bmf-tech.com • URLϧʔςΟϯάΛͭ͘ΔɹΤϐιʔυ1 • https://bmf-tech.com/posts/ URL%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%82%92%E3%81%A4%E3% 81%8F%E3%82%8B%E3%80%80%E3%82%A8%E3%83%94%E3%82%BD%E3%83%BC%E3%83%891 •

    URLϧʔςΟϯάΛͭ͘ΔɹΤϐιʔυ2 • https://bmf-tech.com/posts/ URL%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%82%92%E3%81%A4%E3% 81%8F%E3%82%8B%E3%80%80%E3%82%A8%E3%83%94%E3%82%BD%E3%83%BC%E3%83%892 • URLϧʔςΟϯάΛͭ͘ΔɹΤϐιʔυ3ʢ׬݁ฤʣ • https://bmf-tech.com/posts/ URL%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%82%92%E3%81%A4%E3% 81%8F%E3%82%8B%E3%80%80%E3%82%A8%E3%83%94%E3%82%BD%E3%83%BC%E3%83%893%EF%BC%88% E5%AE%8C%E7%B5%90%E7%B7%A8%EF%BC%89 • Speakerdeck • URLϧʔςΟϯάΛͭ͘ΔΤϐιʔυ1 • https://speakerdeck.com/bmf_san/urlruteinguwotukuruepisodo1
  51. Thank you !