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

2021 Web アプリフレームワーク入門_REST API 入門

kumaki
June 14, 2021

2021 Web アプリフレームワーク入門_REST API 入門

GTB2020 Web App Framework and REST API Introduction

kumaki

June 14, 2021
Tweet

More Decks by kumaki

Other Decks in Programming

Transcript

  1. 1೔໨ʢ2021/05/12 16:15ʙ18:40ʣ ࠲ֶ 1࣌ؒ15෼ (16:15ʙ17:30) kumak1 - Webʹ͍ͭͯ - ϖʔδ͕දࣔ͞ΕΔ·Ͱ

    - WebΞϓϦέʔγϣϯʹ͍ͭͯ - WebΞϓϦέʔγϣϯΛ࡞੒͢Δʹ͋ͨͬͯ ։ൃ؀ڥߏங 1࣌ؒ (17:40ʙ18:40) endu - ConoHa VPSʹαʔόʔΛ௥Ճ͢Δ - VSCodeͷ֦ுػೳRemote-SSHͰαʔόʔʹΞΫηε͢Δ ߨٛͷਐΊํʹ͍ͭͯ 9 εέδϡʔϧͱ୲౰ʹ͍ͭͯ
  2. Webʹ͍ͭͯ Web ͷؔ࿈ٕज़͸ͱͯ΋ѻ͍΍͘͢ɺ༷ʑͳͱ͜ΖͰར༻͞Ε͍ͯ·͢ɻ - Chrome ΍ Edge ͳͲͷϒϥ΢βͰاۀ΍αʔϏεͷWebϖʔδΛݟΔ - ΦʔιυοΫεͳ࢖ΘΕํ

    - PCɾεϚϗɾςϨϏͱ͍༷ͬͨʑͳσόΠε͔ΒݟΔ͜ͱ͕Ͱ͖Δ - εϚϗͷήʔϜΞϓϦͰ௨৴Λߦ͍ͳ͕ΒڠྗϓϨΠΛ͢Δ - ϒϥ΢βҎ֎ͷ༷ʑͳΞϓϦ΋σʔλͷ΍ΓऔΓʹར༻͍ͯ͠Δ Webʹ͍ͭͯ 20
  3. ΫϥΠΞϯτͱαʔόʔ αʔϏεΛఏڙ͢ΔଆΛαʔόʔɺαʔϏεΛड͚ΔଆΛΫϥΠΞϯτͱ ݺͼ·͢ʢҰൠతͳݴ༿ʹͳΓͭͭ͋Γ·͢Ͷʣɻ ΫϥΠΞϯτͷ୅දྫ͕ϒϥ΢β - Google Chrome - Microsoft EdgeɺInternet

    Explorer - Apple Safari - Mozilla Firefox αʔόʔͱͯ͠ར༻͞Ε͍ͯΔ୅දతͳ΋ͷ - Apache HTTP Server - Nginx (ΤϯδϯΤοΫε) - Microsoft IIS (Internet Information Services) - LiteSpeed Web Server Webʹ͍ͭͯ 21
  4. URL ͱ HTTP Web Ͱ͸࣍ͷ2͕ͭͱͯ΋ॏཁͳཁૉͱͳΓ·͢ɻ URL (Uniform Resource Locator) ʮཉ͍͠৘ใ͕Ͳ͜ʹ͋Δͷ͔ʯͱ͍͏৔ॴΛ໌֬ʹࣔ͢

    HTTP (HyperText Transfer Protocol) URL Ͱࣔͨ͠৔ॴʹରͯ͠ʮ৘ใΛͲ͏͍͔ͨ͠ʯΛ௨৴Ͱࢦࣔ͢Δ ্هͷ2ͭΛ͜ΕҎ߱΋͏গ͚ͩ͠ਂ۷Γ͠·͢ɻ Webʹ͍ͭͯ 22
  5. HTTP ͷྑ͞ HTTP ͸ͦΕ·Ͱ͋ͬͨϓϩτίϧΑΓ͔ͳΓ୯७ͳ΋ͷͱͳ͍ͬͯ·͢ɻ Ҏલͷϓϩτίϧ - SMTP ΍ POP3/IMAP4 (ϝʔϧͰ࢖༻)

    - FTP (ϑΝΠϧసૹͰ࢖༻) ౳ - ෳ਺ͷ໋ྩΛ૊Έ߹Θͤͯ໨తͷಈ࡞Λߦ͏ HTTP - (جຊతʹ) 1ͭͷ໋ྩͰ໨తΛՌͨͤΔ - ϓϩτίϧ͕୯७ͳͨΊɺαʔόʔ΍ΫϥΠΞϯτΛ࣮૷͠΍͍͢ HTTP Λར༻ͨ͠ϓϩμΫτ΍ࣄྫ͕രൃతʹ૿͑·ͨ͠ɻ Webʹ͍ͭͯ 25
  6. HTTP ͷ΍ΓऔΓ ྫ͑͹ɺYahoo! JAPAN ͷϖʔδΛදࣔ͢Δ·ͰΛߟ͑ͯΈ·͠ΐ͏ɻ 1. ϒϥ΢β͔Β Yahoo! JAPAN ͷWebαʔόʔʹτοϓϖʔδͷ৘ใΛ΋Β͏ͨΊʹ

    τοϓϖʔδͷURL ʹରͯ͠ϦΫΤετΛ౤͛Δ 1. Webαʔόʔ͸τοϓϖʔδͷσʔλΛ४උͯ͠ɺϒϥ΢βʹϨεϙϯεͱͯ͠ HTML ϑΝΠϧΛฦ͢ 2. ϒϥ΢β͕ HTML ϑΝΠϧΛղੳ͢ΔͱɺCSS ΍ JavaScriptɺը૾ͱ͍ͬͨϑΝΠϧΛ ௥ՃͰऔಘ͢Δඞཁ͕͋Δ͜ͱ͕൑໌͢Δ 1. ϒϥ΢β͔ΒWebαʔόʔʹ ඞཁͳ΋ͷΛ΋Β͏ͨΊʹ౤͛Δ 2. αʔόʔ͔Βϒϥ΢βʹϦΫΤετ͞Εͨ΋ͷΛฦ͢ ...ҎԼ͜Εͷ܁Γฦ͠ Webϖʔδ͕දࣔ͞ΕΔ·Ͱ 28
  7. ϒϥ΢β͔Β Yahoo! JAPAN ͷWebαʔόʔʹτοϓϖʔδͷ৘ใΛ ΋Β͏ͨΊʹτοϓϖʔδͷURL ʹରͯ͠ϦΫΤετΛ౤͛·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ1ʣ ϦΫΤετ

    GET https://www.yahoo.co.jp/ HTTP/1.1 Host: www.yahoo.co.jp Connection: keep-alive accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/ webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 accept-encoding: gzip, deflate, br accept-language: ja,en-US;q=0.9,en;q=0.8 cookie: ... sec-fetch-dest: document sec-fetch-mode: navigate sec-fetch-site: none sec-fetch-user: ?1 upgrade-insecure-requests: 1 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/ 537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 30
  8. ϒϥ΢β͔Β Yahoo! JAPAN ͷWebαʔόʔʹτοϓϖʔδͷ৘ใΛ ΋Β͏ͨΊʹτοϓϖʔδͷURL ʹରͯ͠ϦΫΤετΛ౤͛·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ1ʣ ϦΫΤετ

    GET https://www.yahoo.co.jp/ HTTP/1.1 Host: www.yahoo.co.jp Connection: keep-alive accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/ webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 accept-encoding: gzip, deflate, br accept-language: ja,en-US;q=0.9,en;q=0.8 cookie: ... sec-fetch-dest: document sec-fetch-mode: navigate sec-fetch-site: none sec-fetch-user: ?1 upgrade-insecure-requests: 1 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/ 537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 GET https://www.yahoo.co.jp/ HTTP/1.1 31
  9. ϒϥ΢β͔Β Yahoo! JAPAN ͷWebαʔόʔʹτοϓϖʔδͷ৘ใΛ ΋Β͏ͨΊʹτοϓϖʔδͷURL ʹରͯ͠ϦΫΤετΛ౤͛·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ1ʣ ϦΫΤετ

    GET https://www.yahoo.co.jp/ HTTP/1.1 Host: www.yahoo.co.jp Connection: keep-alive accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/ webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 accept-encoding: gzip, deflate, br accept-language: ja,en-US;q=0.9,en;q=0.8 cookie: ... sec-fetch-dest: document sec-fetch-mode: navigate sec-fetch-site: none sec-fetch-user: ?1 upgrade-insecure-requests: 1 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/ 537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 GET https://www.yahoo.co.jp/ HTTP/1.1 ϦΫΤετ ϝιου URL HTTPͷ όʔδϣϯ 32
  10. ϦΫΤετϝιου ϦΫΤετϝιου͸ɺURLʹରͯ͠ͲͷΑ͏ͳॲཧΛ͍ͨ͠ͷ͔Λ ࣔͨ͠΋ͷͰ͢ɻ GET - ৘ใΛऔಘ - ෭࡞༻͸ͳ͍Α͏ʹ POST -

    ৘ใΛมߋ PUT - ৘ใΛஔ׵ PATCH - ৘ใΛҰ෦ஔ׵ DELETE - ৘ใΛ࡟আ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ1ʣ 33
  11. Webαʔόʔ͸τοϓϖʔδͷσʔλΛ४උͯ͠ɺϒϥ΢βʹ Ϩεϙϯεͱͯ͠ html ϑΝΠϧΛฦ͠·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ2ʣ Ϩεϙϯε HTTP/1.1 200

    OK Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: text/html; charset=UTF-8 Date: Mon, 20 Apr 2020 06:59:26 GMT Expires: -1 Pragma: no-cache Set-Cookie: B=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pat h=/; domain=.yahoo.co.jp Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Vcap-Request-Id: 075691ed-bab6-4077-58f0-34523d132299 X-Xss-Protection: 1; mode=block Age: 0 Transfer-Encoding: chunked Connection: keep-alive Via: http/1.1 edge1675.img.bbt.yahoo.co.jp (ApacheTrafficServer [c sSf ]) Server: ATS Set-Cookie: XB=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pa th=/; domain=.yahoo.co.jp; secure; samesite=none <!DOCTYPE html><html lang="ja">... 35
  12. Webαʔόʔ͸τοϓϖʔδͷσʔλΛ४උͯ͠ɺϒϥ΢βʹ Ϩεϙϯεͱͯ͠ html ϑΝΠϧΛฦ͠·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ2ʣ Ϩεϙϯε HTTP/1.1 200

    OK Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: text/html; charset=UTF-8 Date: Mon, 20 Apr 2020 06:59:26 GMT Expires: -1 Pragma: no-cache Set-Cookie: B=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pat h=/; domain=.yahoo.co.jp Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Vcap-Request-Id: 075691ed-bab6-4077-58f0-34523d132299 X-Xss-Protection: 1; mode=block Age: 0 Transfer-Encoding: chunked Connection: keep-alive Via: http/1.1 edge1675.img.bbt.yahoo.co.jp (ApacheTrafficServer [c sSf ]) Server: ATS Set-Cookie: XB=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pa th=/; domain=.yahoo.co.jp; secure; samesite=none <!DOCTYPE html><html lang="ja">... HTTP/1.1 200 OK 36
  13. Webαʔόʔ͸τοϓϖʔδͷσʔλΛ४උͯ͠ɺϒϥ΢βʹ Ϩεϙϯεͱͯ͠ html ϑΝΠϧΛฦ͠·͢ɻ Yahoo! JAPAN ͷϖʔδ͕ݟ͑Δ·Ͱʢͦͷ2ʣ Ϩεϙϯε HTTP/1.1 200

    OK Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: text/html; charset=UTF-8 Date: Mon, 20 Apr 2020 06:59:26 GMT Expires: -1 Pragma: no-cache Set-Cookie: B=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pat h=/; domain=.yahoo.co.jp Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Vcap-Request-Id: 075691ed-bab6-4077-58f0-34523d132299 X-Xss-Protection: 1; mode=block Age: 0 Transfer-Encoding: chunked Connection: keep-alive Via: http/1.1 edge1675.img.bbt.yahoo.co.jp (ApacheTrafficServer [c sSf ]) Server: ATS Set-Cookie: XB=18kikclf9qi2e&b=3&s=2m; expires=Thu, 21-Apr-2022 06:59:26 GMT; pa th=/; domain=.yahoo.co.jp; secure; samesite=none <!DOCTYPE html><html lang="ja">... HTTP/1.1 200 OK HTTPͷ όʔδϣϯ εςʔλε ίʔυ εςʔλε ςΩετ 37
  14. εςʔλείʔυ εςʔλείʔυ͸ɺαʔόʔͷॲཧ݁ՌΛࣔͨ͠΋ͷͰ͢ɻ 3ܻͷ਺஋ͰͲͷΑ͏ͳঢ়ଶʹͳ͔ͬͨΛද͠·͢ɻ - 200൪୆͸ॲཧ͕੒ޭͨ͜͠ͱΛද͢ - 200 OK - 201

    Created - 300൪୆͸௥Ճͷॲཧ͕ඞཁͰ͋Δ͜ͱΛද͢ - 301 Moved Permanently - 302 Found - 307 Temporary Redirect - 308 Permanent Redirect εςʔλείʔυ 38
  15. εςʔλείʔυ - 400൪୆͸ϦΫΤετʹ໰୊͕͋ͬͨ͜ͱΛද͢ - 400 Bad Request - 401 Unauthorized

    - ೝূ͕ඞཁ - 403 Forbidden - 404 Not Found - 500൪୆͸αʔόʔଆʹ໰୊͕͋ͬͨ͜ͱΛද͢ - 500 Internal Server Error - 503 Service Unavailable εςʔλείʔυ 39
  16. ϦΫΤετ΋Ϩεϙϯε΋୯ͳΔςΩετͳͷͰɺͦͷ··Ͱ͸؆୯ʹ த਎ΛݟΒΕͯ͠·͍·͢ɻ TLS (Transport Layer Security) - ௨৴૬खͷೝূɺ௨৴ͷ҉߸ԽΛߦ͏ϓϩτίϧ - Ҏલ͸

    SSL (Secure Sockets Layer) ͱݺ͹Ε͍ͯͨ - SSL ͷݹ͍όʔδϣϯͰ࢖ΘΕ͍ͯͨ҉߸ํ͕ࣜഁΒΕͯ͠·ͬͨͨΊɺSSL ͷ্Ґن໿ͱͯ͠ TLS ͕࡞ΒΕͨ - SSL ͱ TLS Λ·ͱΊͯ SSL ͱݺ͹ΕΔ͜ͱ΋ଟ͍ HTTPS - SSL/TLS ϓϩτίϧͰ઀ଓͷ্Ͱ HTTP ௨৴Λߦ͏͜ͱ - SSL/TLS ͕࢖ΘΕ͍ͯͳ͍ঢ়ଶͩͱURL͸ http://ʙ - SSL/TLS ͕࢖ΘΕ͍ͯΔঢ়ଶͰ͸ https://ʙ ʹͳΔ ҆શͳ௨৴Λ͢ΔͨΊʹ 41
  17. ಛఆͷ୭͔͚ͩར༻͍ͤͨ͞ ఏڙ͢ΔίϯςϯπʹΑͬͯ͸ɺձһ͚͕ͩ࢖͑Δͱ͔ɺಛఆͷਓ͚͕ͩ࢖͑Δ ͱ੍͍ͬͨݶΛ͍ͨ͜͠ͱ͕Α͋͘Γ·͢ɻ ೝূ (Authentication) - ୭Ͱ͋Δ͔Λ֬ೝͯ͠ɺڐՄ͞ΕͨਓҎ֎͸ڐ͞ͳ͍ - ྫɿiPhone ͷ

    Face ID Λ༻͍ͨೝূͳͲ ೝՄ (Authorization) - ୭Ͱ͋Δ͔ʹԠͯ͡ɺͲͷΑ͏ͳݖར͕͋Δ͔Λίϯτϩʔϧ͢Δ - ྫɿөըؗͰόΠΦϨϯεͳөըΛΈΔͳͲ ೔ຊޠͰ΋ࣅͨΑ͏ͳ༻ޠͩ͠ɺӳޠͰ΋྆ํͱ΋ུ͢ͱ Auth ͱͳΓɺࠞಉ͞ΕΔࣄ͕ଟ͍ͷͰ஫ҙ͠·͠ΐ͏ɻ 42
  18. υΩϡϝϯτϧʔτ - URL (https://<server_name>/) ͷτοϓ෦෼ͱରԠ͢Δαʔόʔ্ͷσΟϨΫτϦͷ͜ͱ - ≒υΩϡϝϯτϧʔτʹϑΝΠϧΛஔ͘ͱWebʹެ։͞ΕΔ - Linux ΛWebαʔόʔʹηοτΞοϓ͢Δͱ

    /var/www/html ͕υΩϡϝϯτϧʔτʹͳΔ - ઃఆϑΝΠϧͰมߋՄೳ - ϨϯλϧαʔόʔͰ͸1ͭͷαʔόʔΛෳ਺ͷ͓٬༷͕ར༻͢ΔͷͰɺ͓٬༷ͷυϝΠϯຖʹ υΩϡϝϯτϧʔτΛઃఆ͍ͯ͠Δ υΩϡϝϯτϧʔτʹϑΝΠϧΛແ଄࡞ʹஔ͘ͱɺWebϒϥ΢β͔Β؆୯ʹΞΫηε͞Εͯ͠·͏ɻ ͜ͷͨΊɺݟΒΕͯ͸͍͚ͳ͍σʔλ͸ /var/www/data ͳͲͷυΩϡϝϯτϧʔτ֎ͷσΟϨΫτϦʹ ஔ͘Α͏ʹ͠·͠ΐ͏ʢ৘ใ࿙͍͑ͷҰ෦͸υΩϡϝϯτϧʔτʹޡͬͯϑΝΠϧΛஔ͍ͨ͜ͱͰൃੜ͍ͯ͠·͢ʣɻ ิ଍ 43
  19. Web ΞϓϦέʔγϣϯΛ෼ྨͯ͠ΈΔ WebϖʔδΛදࣔ͢ΔͨΊͷΞϓϦέʔγϣϯ - αʔόʔ͔Β͸ HTML, JavaScript, CSS, ը૾ͳͲΛฦ͢ -

    ݹ͔͘Βఏڙ͞Ε͖ͯͨ΋ͷ εϚϗ౳͔Βར༻͞ΕΔΞϓϦέʔγϣϯ - εϚϗΞϓϦ౳͔ΒΞΫηε͞Εɺσʔλͷऔಘ΍ߋ৽Λߦ͏ - ʮσʔλΛͲ͏΍ΓऔΓΛ͢Δ͔ʯͱ͍͏ܾ·ΓΛΞϓϦέʔγϣϯຖʹܾΊͯɺ ΫϥΠΞϯτͱαʔόʔͰ΍ΓऔΓΛߦ͏ - ͜ͷܾ·Γͷ͜ͱΛ API (Application Programming Interface) ͱ͍͏ ࠓճͷߨٛͰ͸͜ͷ API ͷ࣮૷ํ๏Λֶͼ·͢ɻ WebΞϓϦέʔγϣϯʹ͍ͭͯ 46
  20. RESTful API API ͷઃܭํ๏ʹ͸͍͔ͭ͘ͷྲّྀ͕͋Γ·͢ʢRPC ΍ SOAPͳͲʣɻ Web API ͸ SOAP

    (Simple Object Access Protocol) ͳͲͰఏڙ͞Ε͍ͯͨ - γεςϜຖʹෳࡶͳ࢓༷ॻ (XMLͷߏ଄ͳͲ) ͷ΍ΓऔΓ͕ඞཁ - ࡞੒͢ΔγεςϜ͕৭ʑͰ͖Δ͕ෳࡶʹͳΓ͕ͪ RESTful API - URL ͕ࣔ͢΋ͷʹରͯ͠ HTTP ͷϝιουΛ࢖ͬͯ୯७ʹΞΫηε͢Δ - ࢓༷͕γϯϓϧʹͳΔͷͰɺෳ਺ͷγεςϜ࿈ܞ͕ൺֱత༰қʹͳΔ ࡢࠓ͸ RESTful API Ͱఏڙ͢Δ΋ͷ͕ଟ͍Ͱ͢ɻ WebΞϓϦέʔγϣϯʹ͍ͭͯ 47
  21. REST ͱ͍͏ߟ͑ํ Web ͰγεςϜΛߏங͢Δʹ͋ͨͬͯɺͲͷΑ͏ʹ͢Δͱޮ཰͕͍͍ͷ͔ͱ ͍͏ߟ͑ํ͕ HTTP ͷن໿Λࡦఆͨ͠ϝϯόʔͷҰਓͰ͋Δ Roy Fielding ʹΑͬͯఏএ͞Ε·ͨ͠ɻ

    REST (REpresentational State Transfer) - ֦ுੑͷ͋Δ Web γεςϜΛߏங͢ΔͨΊͷ ΞʔΩςΫνϟελΠϧ(ઃܭͷྲّྀ΍༷ࣜɺ࡞๏) - ΫϥΠΞϯτͱαʔόʔͷؒΛURLͱ౷ҰΠϯλϑΣʔε (ϦΫΤετϝιου) Λ࢖ͬͯ εςʔτϨεʹ΍ΓऔΓ͢Δ͜ͱʹΑΓ֦ுੑΛߴΊΔΑ͏ʹͰ͖Δͱͨ͠ߟ͑ - Web ͓Αͼ HTTP ͦͷ΋ͷͷݪଇͱݴ͑Δ WebΞϓϦέʔγϣϯʹ͍ͭͯ 48
  22. CRUD ͱ͍͏ߟ͑ํ CRUD ͱ͸ɺσʔλૢ࡞Ͱ࢖͏4ͭͷجຊػೳͷͦΕͧΕͷ಄จࣈΛͱͬͨ΋ͷͰ ͢ɻ͜ΕΛ RESTful API ʹϚοϐϯά͢ΔͱҎԼͷ௨ΓͰ͢ɻ Create -

    σʔλΛੜ੒͢Δ - POST <URL> Read - σʔλΛಡΈऔΔ - GET <URL> Update - σʔλΛߋ৽͢Δ - PUT <URL> or PATCH <URL> Delete - σʔλΛ࡟আ͢Δ - DELETE <URL> WebΞϓϦέʔγϣϯʹ͍ͭͯ 49
  23. ฦ٫͞ΕΔσʔλͷܗࣜ ௨ৗͷWebΞϓϦέʔγϣϯͰ͸ HTML ΍ JavaScriptɾCSS ͳͲ͕ฦ٫ ͞Ε·͕͢ɺAPI Ͱ͸ͦΕͱ͸ҧͬͨܗࣜͰσʔλΛฦ٫͠·͢ɻ ύʔε -

    ϓϩάϥϜͰσʔλ (จࣈྻ) ղੳ͢Δ͜ͱ - API Ͱѻ͏σʔλ͸ϓϩάϥϜͰॲཧ͠΍͍͢ܗࣜΛ બͿʢHTML ΍୯ͳΔෳ਺ߦςΩετ͸ύʔεͮ͠Β͍ʣ ύʔε͠΍͍͢σʔλܗࣜ - XML (Extensible Markup Language) - σʔλʹରͯ͠ݫີʹλάΛ෇༩ͨ͠σʔλܗࣜ - JSON (JavaScript Object Notation) - JavaScript Ͱͦͷ··ར༻ՄೳͰɺͦͷଞݴޠͰ΋ѻ͍΍͍͢ - ࠓճͷߨٛͰ͸ JSON ΛऔΓѻ͏ WebΞϓϦέʔγϣϯʹ͍ͭͯ 50
  24. JSON αϯϓϧίʔυͰൺֱ WebΞϓϦέʔγϣϯʹ͍ͭͯ XML HTML <!doctype html> <html> <head> <meta

    charset="utf-8"> </head> <body> <form> <input
 type="text"
 name="key"
 value="value"> </form> </body> </html> { "key": "value" } <?xml version="1.0" encoding="utf-8" ?> <key>value</key> 51
  25. ϑϨʔϜϫʔΫͱ͸ ໌೔ͷϋϯζΦϯͰ࡞੒͢ΔWebΞϓϦέʔγϣϯͰ͸ϑϨʔϜϫʔΫΛ ར༻͠·͢ɻ ϑϨʔϜϫʔΫʢWeb ΞϓϦέʔγϣϯϑϨʔϜϫʔΫʣ - ϞϊΛ࡞੒͢Δࡍɺ0͔Βશͯ࡞Δͷ͸ͱͯ΋େม - ౔୆ͱͯ͠Կׂ͔Ͱ͖͕͋ͬͨϞϊͷ্ʹॲཧΛ௥Ճ͢Δ͜ͱͰɺΞϓϦέʔγϣϯ׬੒·Ͱͷ खؒΛগͳ͘͢Διϑτ΢ΣΞ

    - ϑϨʔϜϫʔΫຖʹϧʔϧ΍࡞๏͕͋Γɺ͜Εʹै͑͹ൺֱత؆୯ʹΞϓϦέʔγϣϯΛ ࡞੒Ͱ͖Δʢٯʹݴ͑͹ϑϨʔϜϫʔΫຖʹบ͕͋Γɺ߹Θͤͯ࡞Δඞཁ͕͋Δʣ ϥΠϒϥϦͱͷҧ͍ - ϥΠϒϥϦ͸ࣗ෼͕࡞੒͢ΔϓϩάϥϜ͔Βར༻͢Δ෦඼ - ಛผͳॲཧΛ؆୯ʹ࣮ݱͰ͖ΔΑ͏࡞੒͞Ε͓ͯΓɺ࠶࣮૷͢Δख͕ؒল͚Δ - ϑϨʔϜϫʔΫ͸ࣗ෼͕࡞੒͢ΔϓϩάϥϜ͕ར༻͞ΕΔ෦඼ WebΞϓϦέʔγϣϯΛ࡞੒͢Δʹ͋ͨͬͯ 53
  26. ύλʔϯ ϓϩάϥϜΛઃܭɾ࣮૷ʹ͓͍ͯʮ୭΋͕ࣅ࣮ͨ૷Λ͍ͯ͠Δͳʯͱ ͍͏͜ͱ͕͠͹͠͹ى͜Γ·͢ɻ ύλʔϯ - ࣅͨΞϓϦέʔγϣϯΛ࡞੒͢Δ৔߹ɺޙʑ֦ுɾमਖ਼͠΍͍͢ڞ௨ͷ΍Γํ͕ग़ͯ͘Δ - ख๏΍खॱʹ໊લΛ͚ͭɺΤϯδχΞؒͰڞ௨ͷޠኮͱͯ͠ձ࿩͕Ͱ͖ΔΑ͏ʹͨ͠Ϟϊ Ξϯνύλʔϯ -

    ύλʔϯ͸ྑ͍Ϟϊ͹͔ΓͰ͸ͳ͍ - ʮ͜͏͍͏෩ʹ͢Δͱྑ͘ͳ͍Αʯͱ஫ҙשى͢Δ΋ͷ ϑϨʔϜϫʔΫ͸͜ͷύλʔϯΛଟ͘ར༻͓ͯ͠Γɺͦͷ୅දྫ͕ MVCύλʔϯͰ͢ɻ WebΞϓϦέʔγϣϯΛ࡞੒͢Δʹ͋ͨͬͯ 54
  27. MVCύλʔϯ MVCύλʔϯ (Model-View-Controller ύλʔϯ) - ModelɺViewɺController ͷ3ͭͷύʔπͰΞϓϦέʔγϣϯΛ࡞Δߟ͑ํ Controller - ֎෦͔ΒͷϦΫΤετΛड͚Δ

    - ॲཧΛ Model ʹґཔͯ͠ɺ݁ՌΛड͚औΔ - ॲཧ݁ՌΛ View ʹ౉ͯ͠ɺग़ྗ݁ՌΛड͚औΔ - ग़ྗ݁ՌΛϨεϙϯεͱͯ͠ฦ٫͢Δ Model - ΞϓϦέʔγϣϯͱͯ͠ͷॲཧΛ୲౰͢Δ - DBΞΫηε΍֎෦αʔϏεͱͷ΍ΓऔΓ͸͜͜Ͱߦ͏ View - ॲཧ݁ՌΛͲͷΑ͏ʹΞ΢τϓοτ͢Δ͔Λ୲౰͢Δ - HTMLͷੜ੒΍ɺJSONͷੜ੒ΛߦͬͨΓ͢Δ WebΞϓϦέʔγϣϯΛ࡞੒͢Δʹ͋ͨͬͯ 55
  28. MVCύλʔϯͷಛ௃ MVCύλʔϯ͸ଟ͘ͷϑϨʔϜϫʔΫʹ࠾༻͞Ε͍ͯΔͨΊԠ༻͠΍͍͢ - ࣮૷͢Δࡍʮ MVC ͷͲΕ͕୲౰͢Δ͔ʯͱ͍͏͜ͱͰ੔ཧ͠΍͍͢ - σΟϨΫτϦߏ੒ʢͲ͜ʹԿΛஔ͔͘ʣ͸೰ΈͷλωʹͳΓ΍͍͢ - ΄ͱΜͲͷϑϨʔϜϫʔΫ͸σΟϨΫτϦߏ੒͕ܾ·͓ͬͯΓ

    ͜Εʹै࣮ͬͯ૷͢ΔͷͰ ʢൺֱతʣ ೰·ͳ͍ͰࡁΉ ஫ҙ఺ - MVCͷ୲౰ΛׂΓৼΔࡍɺͲ͏ͯ͠΋ᐆດͳ෦෼͕Ͱ͖ͯ͠·͏ - Controller, Model ͷͲͪΒ͔ͷ୲౰ൣғ͸޿͘ͳΓ͗͢Δ܏޲͕͋Δ WebΞϓϦέʔγϣϯΛ࡞੒͢Δʹ͋ͨͬͯ 56
  29. ͜Ε͔Βߦ͏۩ମతͳखॱ 1. VSCodeʹ֦ுػೳ Remote-SSH ΛΠϯετʔϧ͢Δ 2. VSCode Remote-SSH ͷ઀ଓઃఆ a.

    SSH Key ͷݖݶมߋ͢Δ b. VSCode Remote-SSH ʹ઀ଓઃఆΛ௥Ճ͢Δ Remote-SSHͰαʔόʔʹΞΫηε͢Δ 69
  30. αʔόʔͷઃఆ͕ਖ਼͘͠Ͱ͖͍ͯΔ͔֬ೝ͠Α͏ Remote-SSHͰConoHa VPSʹΞΫηε͢Δ ࡞ۀ root@αʔόʔ໊ ͱදࣔ͞Ε͍ͯΔλʔϛφϧͰ࡞ۀ͠·͢ cd /var/www/html pwd #

    ͜ͷΑ͏ʹදࣔ͞Ε͍ͯΔ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ /var/www/html ΋͠Τϥʔ͕ग़ͨΓɺ্هͷΑ͏ͳ݁Ռ͕දࣔ͞Εͳ͔ͬͨΒɺ VPSͷॳظઃఆ͕ؒҧ͍ͬͯΔՄೳੑ͕͋Γ·͢ɻ ݱࡏͷΠϯελϯεΛ࡟আͯ͠ɺ࠶౓VPSͷઃఆ͔Β΍Γ௚͠·͠ΐ͏ɻ खΛڍ͛ͯڭ͍͍͑ͯͨͩͨΒϑΥϩʔ͠·͢ʂ
  31. Laravelͷ঺հ PHPͰॻ͔ΕͨϑϨʔϜϫʔΫ - ެࣜυΩϡϝϯτɹ https://laravel.com/ - ೔ຊޠ༁·ͱΊαΠτ https://readouble.com/laravel/ όʔδϣϯʹ͍ͭͯ -

    v1 ͸2011೥6݄ఏڙ։࢝ - ࠷৽όʔδϣϯ͸ 8.Xʢ2021/4 ݱࡏʣ - ൒೥ຖʹϝδϟʔόʔδϣϯΞοϓ - LTSʢLong Term SupportʣͷϦϦʔε͸2೥ຖ - όάϑΟοΫε͸2೥ؒܧଓ - ηΩϡϦςΟϑΟοΫε͸3೥ؒܧଓ ࠓճͷߨٛͰ͸ Laravel 6.Xʢ࠷৽ͷLTSʣΛ࢖༻͠·͢ɻ Laravelͱ͸ʁ 81
  32. ϑϨʔϜϫʔΫͱ͸ ໌೔ͷϋϯζΦϯͰ࡞੒͢ΔWebΞϓϦέʔγϣϯͰ͸ϑϨʔϜϫʔΫΛ ར༻͠·͢ɻ ϑϨʔϜϫʔΫʢWeb ΞϓϦέʔγϣϯϑϨʔϜϫʔΫʣ - ϞϊΛ࡞੒͢Δࡍɺ0 ͔Βશͯ࡞Δͷ͸ͱͯ΋େม - ౔୆ͱͯ͠Կׂ͔Ͱ͖͕͋ͬͨϞϊͷ্ʹॲཧΛ௥Ճ͢Δ͜ͱͰɺΞϓϦέʔγϣϯ׬੒·Ͱͷ

    खؒΛগͳ͘͢Διϑτ΢ΣΞ - ϑϨʔϜϫʔΫຖʹϧʔϧ΍࡞๏͕͋Γɺ͜Εʹै͑͹ൺֱత؆୯ʹΞϓϦέʔγϣϯΛ ࡞੒Ͱ͖Δʢٯʹݴ͑͹ϑϨʔϜϫʔΫຖʹบ͕͋Γɺ߹Θͤͯ࡞Δඞཁ͕͋Δʣ ϥΠϒϥϦͱͷҧ͍ - ϥΠϒϥϦ͸ࣗ෼͕࡞੒͢ΔϓϩάϥϜ ͔Β ར༻͢Δ෦඼ - ಛผͳॲཧΛ؆୯ʹ࣮ݱͰ͖ΔΑ͏࡞੒͞Ε͓ͯΓɺ࠶࣮૷͢Δख͕ؒল͚Δ - ϑϨʔϜϫʔΫ͸ࣗ෼͕࡞੒͢ΔϓϩάϥϜ ͕ ར༻͞ΕΔ෦඼ ෮शʢϑϨʔϜϫʔΫͱ͸ʣ 82
  33. ϑϨʔϜϫʔΫΛ࢖͏σϝϦοτ - ֶशίετ͕૿͑Δ - ϑϨʔϜϫʔΫͷ͓࡞๏ͷशಘ͕ඞཁʢݴޠͷ͓࡞๏ͱผʹʣ - ྫɿίϚϯυϥΠϯΠϯλʔϑΣΠεɺORMͷه๏ɺϧʔςΟϯάͳͲ - ϑϨʔϜϫʔΫͷ஌ࣝʹภͬͯ͠·͏ -

    ࡉ͔͍࢓૊ΈΛཧղͤͣʹΞϓϦέʔγϣϯ͕࡞Εͯ͠·͏ͷͰ ϒϥοΫϘοΫεԽ͠΍͍͢ - ΧελϚΠζͮ͠Β͍ʢεΫϥονͱൺ΂ʣ - ΞϓϦέʔγϣϯͷن໛ʹରͯ͠৑௕ʹͳΔ৔߹͕͋Δ Laravelͱ͸ʁ 85
  34. LaravelͷศརػೳͷҰྫ 88 ArtisanίϚϯυ ॲཧ಺༰ php artisan -v Laravelͷόʔδϣϯͷ֬ೝ php artisan

    list ίϚϯυҰཡΛ֬ೝͰ͖Δ php artisan <࣮ߦίϚϯυ> -h ίϚϯυͷ࢖͍ํΛ֬ೝͰ͖Δ php artisan make:controller FooController FooίϯτϩʔϥʔΛ࡞੒͢Δ php artisan make:model Foo FooϞσϧΛ࡞੒͢Δ php artisan make:migration create_tests_table --create=tests create_tests_table=ϑΝΠϧ໊ɺtests=ςʔϒϧ໊ͰϚΠά ϨʔγϣϯϑΝΠϧΛ࡞੒͢Δ php artisan migrate ϚΠάϨʔγϣϯͷ࣮ߦ php artisan migrate:rollback ϚΠάϨʔγϣϯͷϩʔϧόοΫ php artisan route:list ϧʔςΟϯάͷҰཡΛදࣔ php artisan tinker REPLͷىಈʢͪΐͬͱͨ͠σόοάͱ͔ʹศརʣ
  35. .envϑΝΠϧʹ͍ͭͯ - ΞϓϦέʔγϣϯͰ࢖͏֤छઃఆؚ͕·ΕΔ - σʔλϕʔεͷ઀ଓઃఆ - ΞϓϦέʔγϣϯݻ༗ͷ؀ڥม਺ʢAPP_KEYͳͲʣ - ։ൃ؀ڥ΍ຊ൪؀ڥͷ੾Γସ͑ -

    Ϧετͨ͠஋͸PHPͷεʔύʔάϩʔόϧม਺΁ϩʔυ͞ΕΔ - ൿಗ৘ใͳͲΛ֎෦͔ΒΞΫηεͰ͖ͳ͍ܗͰอ࣋Ͱ͖Δ .envϑΝΠϧ͸GitHubͳͲʹΞοϓϩʔυͯ͠͸͍͚·ͤΜ LaravelͷศརػೳͷҰྫ 90
  36. σΟϨΫτϦͷߏ੒ͷҰ෦ MVCͷσΟϨΫτϦ - app/Http/ ModelͷϑΝΠϧؚ͕·ΕΔ - app/Http/Controller/ ControllerͷϑΝΠϧؚ͕·ΕΔ - app/resources/views/

    ViewͷϑΝΠϧؚ͕·ΕΔ ͦͷଞͷσΟϨΫτϦ - app/Console/Commands/ ArtisanͷಠࣗΧελϜίϚϯυ - routes/ɹ ϧʔτఆٛϑΝΠϧ - database/ DBͷϚΠάϨʔγϣϯɾॳظ஋ઃఆϑΝΠϧͳͲ - tests/ɹ ςετ - vendor/ɹ ϥΠϒϥϦͳͲʢcomposer ʹΑΔґଘύοέʔδղܾʣ ※ϑϨʔϜϫʔΫͷछྨʹΑͬͯߏ੒͸ҟͳΓ·͢ɻ ɹ·ͨɺόʔδϣϯʹΑͬͯ΋ҟͳΔ৔߹͕͋Γ·͢ɻ LaravelͷσΟϨΫτϦߏ੒ 93
  37. MVCύλʔϯ MVCύλʔϯ (Model-View-Controller ύλʔϯ) - ModelɺViewɺController ͷ3ͭͷύʔπͰΞϓϦέʔγϣϯΛ࡞Δߟ͑ํ Controller - ֎෦͔ΒͷϦΫΤετΛड͚Δ

    - ॲཧΛ Model ʹґཔͯ͠ɺ݁ՌΛड͚औΔ - ॲཧ݁ՌΛ View ʹ౉ͯ͠ɺग़ྗ݁ՌΛड͚औΔ - ग़ྗ݁ՌΛϨεϙϯεͱͯ͠ฦ٫͢Δ Model - ΞϓϦέʔγϣϯͱͯ͠ͷॲཧΛ୲౰͢Δ - DBΞΫηε΍֎෦αʔϏεͱͷ΍ΓऔΓ͸͜͜Ͱߦ͏ View - ॲཧ݁ՌΛͲͷΑ͏ʹΞ΢τϓοτ͢Δ͔Λ୲౰͢Δ - HTMLͷੜ੒΍ɺJSONͷੜ੒ΛߦͬͨΓ͢Δ MVCύλʔϯͷ෮श 95
  38. MVCύλʔϯͷಛ௃ MVCύλʔϯ ͸ଟ͘ͷϑϨʔϜϫʔΫʹ࠾༻͞Ε͍ͯΔͨΊԠ༻͠΍͍͢ - ࣮૷͢Δࡍʮ MVC ͷͲΕ͕୲౰͢Δ͔ʯͱ͍͏͜ͱͰ੔ཧ͠΍͍͢ - σΟϨΫτϦߏ੒ʢͲ͜ʹԿΛஔ͔͘ʣ͸೰ΈͷλωʹͳΓ΍͍͢ -

    ΄ͱΜͲͷϑϨʔϜϫʔΫ͸σΟϨΫτϦߏ੒͕ܾ·͓ͬͯΓ ͜Εʹै࣮ͬͯ૷͢ΔͷͰʢൺֱతʣ೰·ͳ͍ͰࡁΉ ஫ҙ఺ - MVCͷ୲౰ΛׂΓৼΔࡍɺͲ͏ͯ͠΋ᐆດͳ෦෼͕Ͱ͖ͯ͠·͏ - Controller, Model ͷͲͪΒ͔ͷ୲౰ൣғ͸޿͘ͳΓ͗͢Δ܏޲͕͋Δ →͔࣍ΒɺLaravelͰMVCΛݟ͍͖ͯ·͢ 96 MVCύλʔϯͷ෮श
  39. Model ECγϣοϓ͔Β෰ͷҰཡσʔλΛऔಘ͍ͨ͠৔߹ͷྫ 102 ॲཧͷྲྀΕΛMVCʹԊͬͯݟͯΈΑ͏ Route DB View ᶄ ᶅ ᶆ

    ᶇ ᶈ ᶉ Controller ControllerଆͰʮ෰ͷҰཡΛ औಘ͢ΔʯॲཧΛݺͼग़͠·͢ ᶃ
  40. Model ECγϣοϓ͔Β෰ͷҰཡσʔλΛऔಘ͍ͨ͠৔߹ͷྫ 103 ॲཧͷྲྀΕΛMVCʹԊͬͯݟͯΈΑ͏ Route DB View ᶃ ᶄ ᶅ

    ᶆ ᶇ ᶈ ᶉ Controller DB͔ΒσʔλΛऔಘ͍ͨ͠ͷͰ Ϟσϧʹॲཧ͕ҠΓ·͢
  41. LaravelͷσΟϨΫτϦͷߏ੒ͷҰ෦ MVCͷσΟϨΫτϦ - app/Http/ ModelͷϑΝΠϧؚ͕·ΕΔʢॾઆ͋Δʣ - app/Http/Controller/ ControllerͷϑΝΠϧؚ͕·ΕΔ - app/resources/views/

    ViewͷϑΝΠϧؚ͕·ΕΔ ͦͷଞͷσΟϨΫτϦ - app/Console/Commands/ ArtisanͷಠࣗΧελϜίϚϯυ - routes/ɹ ϧʔτఆٛϑΝΠϧ - database/ DBͷϚΠάϨʔγϣϯɾॳظ஋ઃఆϑΝΠϧͳͲ - tests/ɹ ςετ - vendor/ɹ ϥΠϒϥϦͳͲʢcomposer ʹΑΔґଘύοέʔδղܾʣ →ઌఔͷॲཧͷྲྀΕʹɺσΟϨΫτϦΛରԠͤͯ͞Έ·͠ΐ͏ LaravelͷσΟϨΫτϦߏ੒ͷ͓͞Β͍ 109
  42. ConoHa VPSʹڭࡐΛμ΢ϯϩʔυ͠Α͏ ࡞ۀ git ΛΠϯετʔϧ͠·͢ɻ sudo yum -y install git

    # git͕Πϯετʔϧ͞Ε͍ͯΔ͜ͱΛ֬ೝ͠·͢ git --version # ੒ޭ git version 1.8.3.1 ΞϓϦέʔγϣϯΛηοτΞοϓ͠Α͏
  43. ConoHa VPSʹڭࡐΛμ΢ϯϩʔυ͠Α͏ ΞϓϦέʔγϣϯΛηοτΞοϓ͠Α͏ ࡞ۀ μ΢ϯϩʔυͨ͠ڭࡐͷσΟϨΫτϦʹҠಈ͠·͢ɻ σΟϨΫτϦΛҠಈɾڭࡐΛμ΢ϯϩʔυ͠·͢ɻ cd /var/www/html git clone

    https://github.com/tosite/gtb-web-application-framework.git cd /var/www/html/gtb-web-application-framework pwd # ͜ͷΑ͏ʹදࣔ͞Ε͍ͯΔ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ /var/www/html/gtb-web-application-framework ΋͠ cd /var/www/html ͰΤϥʔ͕ͰͨΒVPSͷॳظઃఆ͕ؒҧ͍ͬͯΔՄೳੑ͕͋ΔͷͰ खΛڍ͛ͯڭ͍͑ͯͩ͘͞
  44. routes/api.phpΛVSCodeͰ։͍ͯΈΑ͏ɻ CRUDͷCreate - ϧʔςΟϯάʹ௥Ճ͢Δ APIͷCRUDΛ࣮૷͠Α͏ 145 ֬ೝ <?php use Illuminate\Http\Request;

    Route::middleware('api')->group(function () { Route::delete('logs', function () { exec(': > ../storage/logs/laravel.log'); }); Route::get('comments', 'CommentController@index'); Route::post('comments', 'CommentController@store'); });
  45. λʔϛφϧͰartisanίϚϯυΛ࣮ߦͯ͠Έ·͠ΐ͏ɻ Model͸{Ϟσϧ໊}.phpͱ͍͏ϑΝΠϧ໊Ͱ࡞੒͞Ε·͢ɻ Comment Model Λ artisan ίϚϯυͰ࡞੒ APIͷCRUDΛ࣮૷͠Α͏ 151 pwd

    # ͜Ε͕දࣔ͞ΕΔ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ /var/www/html/gtb-web-application php artisan make:model Comment # ੒ޭ Model created successfully. ࡞ۀ
  46. Modelʹ$fillableΛ௥Ճͯ͠nameͱcontentΛ௥Ճ͠·͢ɻ app/Comment.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ Model ʹ $fillable Λ௥Ճ͢Δ APIͷCRUDΛ࣮૷͠Α͏ 154 <?php namespace

    App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = [ 'name', 'content', ]; } ࡞ۀ
  47. app/Http/Controllers/CommentController.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ CRUDͷCreate - ControllerʹstoreͷॲཧΛ௥Ճ APIͷCRUDΛ࣮૷͠Α͏ 155 public function store(\App\Http\Requests\Comments\Post $request)

    { $comment = Comment::create([ 'name' => $request->input('name'), 'content' => $request->input('content'), ]); return response()->json($comment, 201); } ࡞ۀ
  48. ҎԼͷύϥϝʔλΛೖྗޙɺSUBMITΛԡ͍ͯͩ͘͠͞ɻ CRUDͷCreate - ࠶࣮ߦ APIͷCRUDΛ࣮૷͠Α͏ 156 Selected Method post Uri

    /api/comments Parameters name: ࣗ෼ content: ࣗ༝ ֬ೝ 201 ʢDBʹϨίʔυ௥Ճʣ
  49. routes/api.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ CRUDͷRead - ϧʔςΟϯάʹ௥Ճ͢Δ APIͷCRUDΛ࣮૷͠Α͏ 161 <?php use Illuminate\Http\Request; Route::middleware('api')->group(function

    () { Route::delete('logs', function () { exec(': > ../storage/logs/laravel.log'); }); Route::get('comments', 'CommentController@index'); Route::post('comments', 'CommentController@store'); Route::get('comments/{comment}', 'CommentController@show'); }); ࡞ۀ
  50. routes/api.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ CRUDͷUpdate - ϧʔςΟϯάʹ௥Ճ APIͷCRUDΛ࣮૷͠Α͏ 170 <?php use Illuminate\Http\Request; Route::middleware('api')->group(function

    () { Route::delete('logs', function () { exec(': > ../storage/logs/laravel.log'); }); Route::get('comments', 'CommentController@index'); Route::post('comments', 'CommentController@store'); Route::get('comments/{comment}', 'CommentController@show'); Route::put('comments/{comment}', 'CommentController@update'); }); ࡞ۀ
  51. CRUDͷUpdate - ControllerʹupdateͷॲཧΛ௥Ճ app/Http/Controllers/CommentController.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ APIͷCRUDΛ࣮૷͠Α͏ 171 public function update(\App\Http\Requests\Comments\Put $request,

    $id) { $comment = Comment::findOrFail($id); $comment->update([ 'name' => $request->input('name') ?: $comment->name, 'content' => $request->input('content') ?: $comment->content, ]); return response()->json($comment, 200); } ࡞ۀ
  52. routes/api.phpΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ CRUDͷDelete - ϧʔςΟϯάʹ௥Ճ APIͷCRUDΛ࣮૷͠Α͏ 177 <?php use Illuminate\Http\Request; Route::middleware('api')->group(function

    () { Route::delete('logs', function () { exec(': > ../storage/logs/laravel.log'); }); Route::get('comments', 'CommentController@index'); Route::post('comments', 'CommentController@store'); Route::get('comments/{comment}', 'CommentController@show'); Route::put('comments/{comment}', 'CommentController@update'); Route::delete('comments/{comment}', 'CommentController@destroy'); }); ࡞ۀ
  53. app/Http/Controllers/CommentController.php ΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ CRUDͷRead - ControllerʹshowͷॲཧΛ௥Ճ APIͷCRUDΛ࣮૷͠Α͏ 183 public function index()

    { return [ ['name' => 'tom', 'content' => 'hello'], ['name' => 'sam', 'content' => 'world'], ]; return Comment::all(); } ࡞ۀ
  54. σʔλΛऔಘ͢Δ৔߹ͷྫ(SELECT) ҎԼ͸ಉٛͰ͢ɻ SQLΛॻ͔ͣ௚ײతʹDBΛૢ࡞Ͱ͖Δ ORMͷΑ΋΍·࿩ 192 SELECT * FROM comments; SELECT

    * FROM comments where id = 1; App\Comment::all(); App\Comment::where(‘id’, 1)->get(); SQL ORM
  55. σʔλΛ௥Ճ͢Δ৔߹ͷྫ(INSERT) ҎԼ͸ಉٛͰ͢ɻ SQLΛॻ͔ͣ௚ײతʹDBΛૢ࡞Ͱ͖Δ ORMͷΑ΋΍·࿩ 193 INSERT INTO comments VALUE (

    name = ‘tosite’, content = ‘body text’, email = ‘[email protected]’ ); $params = [name => ‘tosite’, content => ‘body text’, email => ‘[email protected]’,]; (new Comments())->fill($params)->save(); SQL ORM
  56. ※੺͘৭Λ෇͚͍ͯΔ෦෼ʹ੬ऑੑ͕જΜͰ͍·͢ɻ ηΩϡϦςΟରࡦ ORMͷΑ΋΍·࿩ 199 $res = pg_query( $dbconn, “select *

    from users where id = ‘{$_POST[‘id’]}’ and password = ‘{$_POST[‘password’]}’”, ); Users::where(‘id’, $_POST[‘id’])->where(‘password’, $_POST[‘password’])->get(); SQL + PHP ORM
  57. ઌ΄ͲͷྫΛݩʹߟ͑ͯΈ·͠ΐ͏ɻ Ωʔϫʔυ͸ʮϢʔβʔ͔ΒͷೖྗΛશ໘తʹ৴͡Δͱ௧͍໨ʹૺ͏ʯͰ͢ɻ ͜ͷΑ͏ʹSQLΛվม͢Δ߈ܸΛʮSQLΠϯδΣΫγϣϯ߈ܸʯͱݴ͍·͢ɻ ηΩϡϦςΟରࡦ ORMͷΑ΋΍·࿩ 201 # ϩάΠϯ࣌ͷೝূͰ࢖༻͢ΔSQLจΛ࣍ͷΑ͏ʹ࡞੒͍ͯͨ͠ͱ͢Δ $res =

    pg_query( $dbconn, “select * from users where id = ‘{$_POST[‘id’]}’ and password = ‘{$_POST[‘password’]}”, ); # Ծʹ$_POST[‘id’]ʹʮ' or '1' = '1';-- ʯͱ͍͏஋͕౉͞Εͨ৔߹ɺҎԼͷΫΤϦ͕ൃߦ͞ΕΔ # ҎԼͷΫΤϦ͸྘৭ͷ෦෼͕ඞͣ੒ཱͯ͠͠·͏ͨΊɺશϢʔβʔͷ৘ใΛݕࡧͰ͖Δ SELECT * FROM users WHERE id = ‘’ or ‘1’ = ‘1’;-- ’ and password = ‘foo’; SQL + PHP
  58. N+1໰୊ΛҾ͖ى͜͢έʔε ORMͷΑ΋΍·࿩ 207 # ORM class Comment extends Model {

    public function user() { return $this->belongsTo('App\User'); } } # Controller $comments = Comment::all(); foreach ($comments as $comment) { echo $comment->user; }
  59. ϖʔδΛදࣔ͢ΔͨͼʹҎԼͷΫΤϦ͕ൃߦ͞ΕΔ͜ͱʹͳΓ·͢ɻ ͜Ε͸ $comments->user ͕࣮ߦ͞ΕΔ౓ʹSQL͕ൃߦ͞ΕΔ͜ͱʹىҼ͠·͢ɻ ΋ͪΖΜɺORMଆͰରࡦ͢Δ͜ͱ͸ՄೳͰ͢ɻ N+1໰୊ΛҾ͖ى͜͢έʔε ORMͷΑ΋΍·࿩ 208 select *

    from comments; select * from users where id = 1; select * from users where id = 2; select * from users where id = 3; -- ҎԼϨίʔυ͕ଘࡏ͢Δ෼͚ͩΫΤϦ͕ൃߦ͞ΕΔ $comments = Comment::with('user')->get(); foreach ($comments as $ comment) { echo $comment->user }
  60. ո͍͠΋ͷ͸ड͚෇͚ͳ͍ - ֎෦͔ΒͷೖྗΛ࢖ͬͯͦͷ··SQLΛ૊ΈཱͯΔ - ʮSQLΠϯδΣΫγϣϯʯ͕ى͖Δ - ֎෦͔ΒͷೖྗΛ࢖࣮ͬͯߦ͢ΔίϚϯυΛ૊ΈཱͯΔ - ʮίϚϯυΠϯδΣΫγϣϯʯ͕ى͖Δ -

    ֎෦͔ΒͷೖྗΛ࢖ͬͯHTMLΛग़ྗͯ͠͠·͏ - ʮΫϩεαΠτεΫϦϓςΟϯά(XSS)ʯ͕ى͖Δ ֎෦͔ΒͷೖྗΛ৴͡ΔͱͲ͏ͳΔͷ͔ʁ ValidationΛ࣮૷͠Α͏ 216
  61. ࠓͷValidationͷ࣮૷ΛΈͯΈΑ͏ app/Http/Request/Comments/Post.php ΛVSCodeͰ։͍ͯΈ·͠ΐ͏ɻ ValidationΛ࣮૷͠Α͏ 220 public function rules() { return

    [ 'name' => 'required', // nameύϥϝʔλ͸ඞਢೖྗ 'content' => 'required', // contentύϥϝʔλ͸ඞਢೖྗ ]; } ֬ೝ
  62. ίϚϯυΛ࣮ߦͯ͠ެ։伴Λొ࿥͠·͢ɻࣄલʹҎԼͷ3ͭΛ֬ೝ͠·͠ΐ͏ɻ - ൿີ伴ͷPath (.ssh/config ʹॻ͍ͯ͋ΔΑ) - ConoHaͷIP (.ssh/config ʹॻ͍ͯ͋ΔΑ) -

    ConoHaͷrootύεϫʔυ ެ։伴Λαʔόʔʹొ࿥͠Α͏ Remote-SSHͰConoHaʹ઀ଓͰ͖ͳ͍ 229 ࡞ۀ # ൿີ伴͕ར༻Ͱ͖ɺެ։伴Λੜ੒Ͱ͖Δ͔֬ೝ͢ΔίϚϯυ ssh-keygen -y -f <ൿີ伴ͷPath>
  63. ެ։伴Λੜ੒Ͱ͖Δ͜ͱΛ֬ೝͨ͠ΒConoHaʹొ࿥͠·͠ΐ͏ɻ ެ։伴Λαʔόʔʹొ࿥͠Α͏ Remote-SSHͰConoHaʹ઀ଓͰ͖ͳ͍ 230 ࡞ۀ # ࣮ߦ͢ΔίϚϯυ ssh-keygen -y -f

    <ൿີ伴ͷPath> | ssh root@<ConoHaͷIP> 'cat >> .ssh/authorized_keys' # ࣮ߦͨ͠ΒrootύεϫʔυΛฉ͔ΕΔͷͰೖྗͯ͠EnterΩʔΛԡ͢ # ໰୊ͳͦ͞͏Ͱ͋Ε͹ɺ࠶ͼConoHaʹssh઀ଓͯ͠ΈΑ͏