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

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

0c9a69560f916778b569086d792680bb?s=128

bmf_san

June 29, 2019
Tweet

Transcript

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

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

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

    ·ͱΊ
  4. 1 ಈػ

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

  6. 2 Ͳ͏࡞͔ͬͨʁ

  7. ࣮ࡍʹ࡞ͬͨ΋ͷ

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

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

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

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

  12. ࣮૷աఔ

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

    ࣮૷
  14. URLϧʔςΟϯάͱ͸Կ͔

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

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

  17. ࢓༷ΛܾΊΔ

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

    ଟػೳ • ϋΠύϑΥʔϚϯε
  19. σʔλߏ଄Λߟ͑Δ

  20. σʔλߏ଄Λߟ͑Δ IUUQFYBNQMFDPNGPPCBS GPPCBS JE͕
 BDUJPO͸ #BS$POUSPMMFS!TIPX 3PVUFS *OQVU 0VUQVU

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

    GET/POST… Output • Action • ॲཧ • ex. PostController@getPosts • Parameters • ύεύϥϝʔλʔ • ex. /boo/bar/:id
  22. Routing Map *OQVU 0VUQVU 3PVUFS 1BUI )551.FUIPE "DUJPO 1BSBNFUFST 3PVUJOH.BQ

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

    ϧʔςΟϯάͷఆ͔ٛΒੜ੒͞ΕΔ • URLͱ࣮ߦ͍ͨ͠ॲཧΛϚοϐϯά
  24. 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
  25. 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
  26. ໦ߏ଄ ɹࠜʢSPPUʣ ɹࢬʢFEHFʣ ɹઅʢOPEFʣ ɹ༿ʢMFBGʣ

  27. σʔλߏ଄ 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
  28. UPLFO &/%@10*/5 &/%@10*/5 &/%@10*/5 &/%@10*/5 QSPpMF  DBUFHPSZ &/%@10*/5 QPTUT

    JE &/%@10*/5 ˞&/%@10*/5͸ޠฐ͕͋Γͦ͏ʜ
  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 
  31. UPLFO &/%@10*/5 &/%@10*/5 &/%@10*/5 &/%@10* /5 QSPpMF  DBUFHPSZ &/%@10*/5

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

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

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

  36. ࣮૷

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

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

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

  40. ࣮૷

  41. ࢀরʢʣͰ͕Μ͹Δ

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

  43. None
  44. ՝୊ • ΤϥʔϋϯυϦϯά • ࣮ӡ༻ʹ޲͚ͨػೳ։ൃ • ϛυϧ΢ΣΞͷͭͳ͗ࠐΈ • ύεύϥϝʔλʹਖ਼نදݱ͕࢖͑Δ •

    ͦͷଞศརػೳͱ͔ • ࣮ߦ଎౓ͷߟྀ • ໦ߏ଄ɺจࣈྻ୳ࡧΞϧΰϦζϜ
  45. 3 ·ͱΊ

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

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

  48. Thank you !