èšäºã®æŠèŠ
Laravelã䜿ã£ãAPIã®å»ºãŠæ¹ãåŠã¹ãŸãã**質ã¯ãšãããã**å€åãããæéã ãšæããŸããïŒãã©ã°ïŒ
泚æäºé
ãããŸã§ããã³ããšã³ããšã³ãžãã¢ã趣å³éçºãšããã©ã€ããŒãã§ã¡ãã£ãšè§Šãå Žåã«ã®ã¿äœ¿ã£ãŠãã ããã
èšäºã®å¯Ÿè±¡
ããã³ããšã³ããã¡ã€ã³ã ãã©ãWebãµãŒãã¹ãäœãäžã§ã¯ããã¯ãšã³ããé¿ããŠéããªãã®ã§ã¡ãã£ãšè§Šã£ãŠã¿ãããäž»æµã®Ruby&RoRãPHP&LaravelãåŠç¿ããŠã¿ããããRoRã¯ãªããçµããã£ãœããïŒèª¹è¬äžå·ïŒãLaravelã«ããšããâŠâŠãšããå±€ãã¿ãŒã²ããã«ããŠããŸãã
å ã«ç¥ã£ãŠããããLaravelã¶ã£ã¡ãã話
éçºæ¹éã«ã€ããŠ
ããã³ããšã³ãžãã¢ã®çããã¯ã
æã¯ããã¯ãšã³ãããHTMLãåºåããã¢ããªã¹ãäž»æµã ã£ããã©ãä»ã¯ããã³ãïŒSPAïŒãšããã¯ïŒAPIïŒãåé¢ããŠéçºããããããã®ãäž»æµ
âŠâŠãšãã䟡å€èŠ³ãæã£ãŠããæ¹ãå€ãããšæããŸãããããæ®å¿µãªããã
Laravelã¯ããã¯ãšã³ãããHTMLãåãåºãã¢ããªã¹è·¯ç·ïŒå¯çµåè·¯ç·ïŒãã¡ã€ã³ã§ãã
ãšããã®ããåçã2011幎6æã«äœããããã§ãããåœæã¯ããã³ã-ããã¯ã®åé¢ã¢ãã«ãæµè¡ãåã ã£ããã§ããããã®ããã©ãããŠãæ ¹åºããã³ã³ã»ããã®åãæ¿ãã¯ãèŸããã¡ã€ã³ã¯ã¢ããªã¹ã ãã©ãAPIãäœããããã«ããâŠâŠãšãã枩床æã§çŸåšãé²åãç¶ããŠããŸãããããæ°å¹ŽãCakePHPã®åŸç¶ãšããŠæµè¡ã£ãŠãã¿ããã ããåºæ¬APIããŒã¹ã§äœãã®æ¥œã
ããðããšæã£ãŠãããã®è²Žæ¹ãæ®å¿µã§ãããåååœãããååå€ããšãã£ããšããã§ãã
æ£çŽãåŠç¿ãé²ããŠãããã¡ã«ãã®èŸºãã®æž©åºŠæãåãã£ãŠããŠããå
è¡ãçã«ã©ããªãã ïŒããšè¥å¹²èºèºããæããããŸãããã©ãããŠãã¢ããªã¹ã£ãŠPHPãRubyããWebpackã©ãããŒã©ã€ãã©ãªãçµç±ããŠWebpackãå©ããŠããã³ãåšãã®ã©ã€ãã©ãªãŸã§ããã¯ã§äžžæ±ããã¿ãããªãæããã«åæãããã ããã¿ãããªæ¹åã«ãªã£ãŠãããã§ããããã ãŸã埡åç¥ã®éãã·ã§ã¢ã倧ããã§ããã次ã«äœããæ¥ãŠããšããããã§ããªãã®ã§âŠâŠã
èšèšã«ã€ããŠ
æåãããã£ãããšèšèšãšãä»çµã¿ãåºæ¥äžãã£ãŠããŠãFWã®äžã«èšå®ãç¬èªããžãã¯ãè¿œå ããŠé¢šè¹ã®ããã«å€§ããããŠããã€ã¡ãŒãžã«ãªããŸããããã³ããšã¯å
šç¶éãæèŠã§ããªãæ°é®®ã§ããã
ãã ãã®ãŸãŸã ãšã³ã³ãããŒã©ãŒãããã«å€ªã£ãããšãã¢ãã«ã倪ã£ãããšããããã«ããããBadãã¿ãŒã³ã«ãªãã®ã§ãã©ãèŠéããã现ååããŠããããèšèšã«ã€ããŠã¯åžžã«æèããããšã«ãªããšæããŸãã
ãŸãããã³ãã§ã¯é¢æ°åã«å¯ããã¹ã¿ã€ã«ãæµè¡ããŠããããã¯ã©ã¹ãªããŠå€ªå€ã®æ代ã®éºç©ã¯äœ¿ããŸãããªãïœð€ïŒéŒ»ããžïŒTSæé«ïŒðããšããããŠãªã€ã±ã€ã±ã€ã³ã°ïŒæ»èªïŒããããããããŸããããLaravelã¯æãåããªããžã§ã¯ãæåã®ãããåãããå¿
èŠããããŸããã©ã¡ãã«ããè¯ããæªãã倧æ ãæ¡ãããŠãã®ã§ãçªã£åŒµã£ãŠãåã¿åããæªãæãããšããããã§ããéãå¯äœçšããé¢æ°ãšçŽç²é¢æ°ããã£ã¡ãåããŠæžããšããããã§ãããâŠâŠãïŒServiceã¯ã©ã¹ãšããåã®staticãªçŽç²é¢æ°ãè©°ã蟌ãã ã¯ã©ã¹çŸ€ãé ããªããïŒ
åŠç¿ã®ããæ¹
PHPèªäœãDBãããšç°å¢æ§ç¯ã§è§Šãããšã«ãªãã ããDockerã«ã€ããŠã¯è§ŠããŸããã
ããã³ããšã³ãžãã¢èŠç¹ã§ã¯ãLaravelã®æ©èœã®ãã¡bladeåšãããããµãªæšãŠãŠãAPIãç«ãŠãããšã«çµã£ãŠåŠç¿ããæµãã«ãªãããšæããŸãã
åŠç¿ã¯å
¬åŒãµã€ãæ¥æ¬èªèš³ã§è¡ãã®ã確å®ã§æã£åãæ©ãã§ãããããã®èšäºã¯ãã®ãµã€ãã®èŠãã¹ãé
ç®ãåªå
床é ã«äžŠã¹ã圢ããšããŸããåãåªå
床ã®å Žåã¯äžããé ã«èŠãŠããã°è¯ãããã«ããŠãããŸãã
ã¡ãªã¿ã«ã³ã³ãLaravelã®ã³ãã¥ããã£ã«ããå
¬åŒã ãšåéãããŠã人ãã¡ããã¡ããããŸãããäžçªäžã®ã³ããŒã©ã€ã衚瀺èŠãéããLaravelã«é¢ããé»åæžç±çãåºãããŠããå·ç¬ è£ä¹
æ°ã«ãããã©ã³ãã£ã¢ã§ããæè¬ããªããèªã¿ãŸãããã
泚æäºé
ã©ã®é
ç®ãé·ãã§ãããæããã«é¢ä¿ããé
ç®ã¯æåããæåŸãŸã§é£ã°ããå
šéšèªãã§ãã ããããæœè±¡çã ã£ããLaravelã®ä»çµã¿ãå€å°åãã£ãŠãªããšç解ã§ããªãæžãæ¹ã§ãå¿
èŠãªäœæ¥ãããã£ãšæžãããŠããããã®ã§æ³šæã
ãšã©ãŒãåºãŠæ£ã
StackOverFlowããLaracastçã§æ€çŽ¢ããŠåã£ãçµæãå®ã¯ããã¥ã¢ã«ã«èšèŒãããŠãŠãããããããã£ãšæžããŠãããã©ãããããæå³ããïœïŒïŒïŒå®äœæ¥ãšããŠäœãããå¿
èŠãããã®ãåãã£ãŠãªãïŒããšå€©ãä»°ãã ããšãæ°åãããŸãð
éèŠåºŠ:â â â â â
ãšããããDBãèšå®âAPIãç«ãŠãŠDBã«èªã¿æžãã§ããããã«ããŸãã
äºåæºå
- å©çšéå§ïŒã€ã³ã¹ããŒã«
- å©çšéå§ïŒèšå®
- å©çšéå§ïŒãã£ã¬ã¯ããªæ§é
- ããŒã¿ããŒã¹ïŒæºå
å¿ èŠãªã®ã§èªãã§ãã ãããããšæåã®ãã¡ã¯ããã®ã¯ã©ã¹ã£ãŠã©ã®ãã£ã¬ã¯ããªã®äžã ã£ãâŠâŠããšãã£ã¬ã¯ããªã®ããªãŒã®äžã§è¿·åã«ãªãããšå¿ è³ãªã®ã§ãæ§é ã«ã€ããŠããã£ããåŠãã§ãããŸãããã
éçº
- åºç€ïŒãã°
- ããŒã¿ããŒã¹ïŒãã€ã°ã¬ãŒã·ã§ã³
- åºç€ïŒã«ãŒãã£ã³ã°
- åºç€ïŒã³ã³ãããŒã©
- Eloquent ORMïŒEloquentã®æºå
- åºç€ïŒã¬ã¹ãã³ã¹
- Eloquent: ã·ãªã¢ã©ã€ãº
DBã®ããŒãã«èšå®ããŠAPIç«ãŠãŠãEroquentçµç±ã§èªã¿æžããããšãã圢ã«ãªããŸãã
éçš
éèŠåºŠ:â â â â
ãã®ãŸãŸã ãšã³ã³ãããŒã©ãŒã«å
šåŠçãåã蟌ãããšã«ãªãã®ã§ãå
¬åŒãæäŸããã¯ã©ã¹åå²ã®æ段ãåŠã³ãŸãã
ãŸãèªåãã¹ãã®åŠç¿ãåŸåãã«ããŠTSïŒSPAãã¬ãŒã ã¯ãŒã¯ã®åŠç¿ã«éäžããŠãããã³ããšã³ãžãã¢ãã¶ã£ã¡ããå€ãããšæããŸãããããã¯ãšã³ãã¯èªåãã¹ãã®éèŠæ§ã段éãã®ããããã®æ®µéã§è§ŠããŸãã
èšèš
DBã®ããŒã¿ã®åãåã
èªåãã¹ã
- ããŒã¿ããŒã¹ïŒã·ãŒãã£ã³ã°
- ãã¹ãïŒãã¹ãã®æºå
- ãã¹ãïŒHTTPãã¹ã
- ãã¹ãïŒããŒã¿ããŒã¹ãã¹ã
éèŠåºŠ:â â â
ãããããèªåã§ãªãããã£ãŠãããŠåããŠããã©ãä»çµã¿ãããåãã£ãŠãªããŠå°ã足ã«ã€ããŠãªããæã匷ããšæãã®ã§ãããŒã¹ãšãªãä»çµã¿ãåŠã³ãŸãããšãããããããŸã§ã®é ç®ã§ãããã£ãšäœ¿ãããŠããããã®ã§ããã£ãŠãå ã«æ°ã«ãªã£ãŠã€ãŸã¿é£ãçã«èªãããšã«ãªãããšã
- æ§ææŠå¿µïŒãµãŒãã¹ã³ã³ãã
- æ§ææŠå¿µïŒãã¡ãµãŒã
- åºç€ïŒããã«ãŠã§ã¢
- DatabaseïŒããžããŒã·ã§ã³
çªå€ïŒèªèšŒåšãã«ã€ããŠ
ããã¯ãšã³ããšèšãã°èªèšŒããšæããŸãããæ®å¿µãªããšã«Laravelã®èªèšŒåšãã®ã©ã€ãã©ãªã¯ä¹±ç«ããŠããŸãã ãããJetstreamãBreezeãæšããŠããŸãããããã¯ç»é¢ä»ãã®ãããããã³ããšã³ãžãã¢çã«ã¯éžæè¢ã«å
¥ããªãããšæããŸãã
çµè«ãããããšãåŠç¿ããå¿
èŠãããã®ã¯3ã€ã§ãã
Laravel Sanctum
ãã©ãŠã¶åãã®Cookieã䜿ã£ãèªèšŒãšãä»ã®ãµãŒããŒãµã€ãããã°ã©ã åãã®APIããŒèªèšŒãäž¡ç«ãã€ã€ãåãã/loginãã®å£ã§åŠçããŠããããšããã©ã€ãã©ãªã«ãªããŸãããã°ã¢ãŠãçã2èŠçŽ èªèšŒããã¹ã¯ãŒããªã»ããçã¯ç¬èªã§å®è£
ããå¿
èŠããããŸãã
ããªãã ããã ããããšæããããããããŸãããããªãŒãã³ãªAPIã®ã¿ãäœããªããšããããå®éã®ãšããèªèšŒãããã䌎ã管çç»é¢ããããã¯ä»ã®ãµãŒããŒãµã€ãããã°ã©ã ãšã®é£æºã¯ããã«å¿
èŠã«ãªã£ãŠããã®ã§ãçµå±å¿
é ã©ã€ãã©ãªã«ãªããŸãã
Laravel Fortify
Sanctumãšæ··åããã¡ã§ããã2èŠçŽ èªèšŒããã¹ã¯ãŒããªã»ããçãèªèšŒåšãã®ããããæ©èœãä»ãå ããã©ã€ãã©ãªãšãªããŸãããã ãããã¯ãšã³ãããHTMLãåºåããåæã§èšèšãããŠãããããèªåã§èšå®ãããAPIãRESTãããªããŠåè©ã«ãªã£ãŠãããªã©ãããããAPI掟ãããããšåŸ®åŠã«ãã°ãã°æåºãŠããŸãã
Laravel Socialite
ãœãŒã·ã£ã«ãã°ã€ã³çšã©ã€ãã©ãªãFortifyãšåãã§ããã¯ãšã³ããHTMLãåºåããåæã§èšèšãããŠããã®ã§ãçµæ§åŒãå¿
èŠããããŸãã
ããããžãã«ãªããšïŒç»é¢åºåããªãå Žåã ãšïŒè±èªã®èšäºããæžã£ãŠããŸãã®ã§ãæ¥æ¬èªã®ããããã®èšäºã貌ã£ãŠãããŸãã
ã©ã€ãã©ãªã«äŸåããããªãïŒåŠç¿ã³ã¹ããéãå ŽåãLaravelã¯èªåã«ããå®è£ ã®æ®µåããããçšåºŠçšæããŠãããŠããŸãããã®ããšãèžãŸããŠãããã¥ã¢ã«ã®ãå®å šãã®åé ç®ããããã¥ã¢ã«å ã®åã©ã€ãã©ãªã®çŽ¹ä»ããŒãžãèªãã§ãã ããããããããåºç€ïŒCSRFä¿è·ããèªãã§ãããæ¹ãããã§ããã
ðLaravelå ¬åŒããã¥ã¡ã³ã翻蚳è ãžã®æ¯æŽã®ãé¡ã
Laravelã®å ¬åŒããã¥ã¡ã³ãã®æ¥æ¬èªçã§ããReaDouble.comãéå¶ãããŠããå·ç¬è£ä¹ æ°ããåºååå ¥ã®äœäžãšæç ã®æªåã®ããã翻蚳ãããžã§ã¯ããç¶ç¶ããããšãå°é£ã«ãªããGitHub sponsorsãåéããŠããŸãã æ¥æ¬ã®LaravelãŠãŒã¶ãŒã§ãããèŠãããšãªã人ã¯ããªããšæãã®ã§ãããæ©çŸ©ãæããŠãããªãã°æ¯éæ¯æŽã®ã»ã©ãé¡ãããŸããç§ãæ¯æŽãããŠããã ããŠãããŸã