PHPのmiddlewareを 使いこなすために
by
YAMAOKA Hiroyuki
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
1)1ͷNJEEMFXBSFΛ ͍͜ͳͨ͢Ίʹ 1)1ΧϯϑΝϨϯεઋ ߹ಉձࣾςϯϚυɹࢁԬ
Slide 2
Slide 2 text
ࣗݾհ ࢁԬ!IJSP@Z 1)1ͱ͔/PEFKTͷΤϯδχΞ 1)1ͷ͜Ζ͔Β ߹ಉձࣾςϯϚυදࣾһ
Slide 3
Slide 3 text
ɹɹ߹ಉձࣾςϯϚυ!! Ϗδωεࢧԉࣄۀ ࣾ֎$50ͱ͔ΞυόΠβʔ *OOPWBUPS+BQBO64&/.FEJBͳͲ αʔϏε։ൃࣄۀ JSVDBNJNFNPDPOBTV
Slide 4
Slide 4 text
ࠓճͷ͓ͷΰʔϧ NJEEMFXBSFͱ͍͏ߟ͑ํ͕Θ͔Δ ฉ͍ͨͱ͖ʹΠϝʔδ͕ු͔Ϳ ར༻γʔϯ͕૾Ͱ͖Δ ϑϨʔϜϫʔΫͰ͑Δ͔Θ͔Δ
Slide 5
Slide 5 text
͓Λ࢝ΊΔલʹ
Slide 6
Slide 6 text
લճ·Ͱͷ͋Β͢͡ 1)1ΧϯϑΝϨϯεؔ 4MJN'SBNFXPSLͰ࢝ΊΔ1)1ͷ NJEEMFXBSF 1)1ΧϯϑΝϨϯε 1)1'*(ͷ)551ॲཧඪ४ͷઃܭ ͳͥ143ʹͳͬͨͷ͔
Slide 7
Slide 7 text
Ͱɺ͋ΒͨΊͯ
Slide 8
Slide 8 text
XFCΞϓϦͱ ϦΫΤετΛड͚औΓʢೖྗʣ ͍Ζ͍ΖॲཧΛͯ͠ ೝূͱ͔%#ͷॲཧͱ͔ ϨεϙϯεΛฦ͢ʢग़ྗʣ
Slide 9
Slide 9 text
؆୯ʹඳ͘ͱ͜͏
Slide 10
Slide 10 text
XFCΞϓϦͷલॲཧ શϦΫΤετڞ௨Ͱ͍ͨ͠ॲཧ ϩάΠϯೝূ %#͔ΒͷσʔλऔಘɹͳͲ ΞϓϦέʔγϣϯຊମͷલʹ ॲཧΛڬΊΔΑ͏ʹ͢ΕΑ͍ͷͰ
Slide 11
Slide 11 text
ϑΟϧλʔʢલॲཧʣ
Slide 12
Slide 12 text
XFCΞϓϦͷޙॲཧ શϦΫΤετڞ௨ͷॲཧɺ·ͩ͋Δ ϩάग़ྗ ॲཧޙʹ·ͱΊͯϝʔϧૹ৴ ΞϓϦέʔγϣϯຊମͷޙʹ ॲཧΛڬΊΔΑ͏ʹͰ͖ΔͱΑͦ͞͏
Slide 13
Slide 13 text
ϑΟϧλʔʢޙॲཧʣ
Slide 14
Slide 14 text
ͯ͞ɺͪΐͬͱ ൃΛస͢Δ
Slide 15
Slide 15 text
ೖΕࢠߏ ೖΕࢠࣜʹॲཧΛ ͭͳ͍Ͱ͍͘ΈΛߟ͑Δ ྫؔ"ͱؔ#Λ࣮͢Δ ؔ"ͦͷॲཧͷதͰؔ#Λ ࣮ߦɺͦͷ݁ՌΛฦ͢Α͏ʹ͢Δ
Slide 16
Slide 16 text
1)1Ͱॻ͍ͯΈΔͱ $a = function (string $str, \Closure $next) : string { echo 'A'; // લॲཧΆ͍ $result = $next($str); // ࣍ΛݺͿ echo 'C'; // ޙॲཧΆ͍ return $result; }; $b = function (string $str) : string { echo 'B'; return strtoupper($str); }; $a('hoge', $b); // ABC
Slide 17
Slide 17 text
ೖΕࢠߏ
Slide 18
Slide 18 text
͞ΒʹೖΕࢠߏ
Slide 19
Slide 19 text
λϚωΪΈ͍ͨʁ QIPUPCZEBSXJO#FMMMJDFOTFEVOEFS$$#: IUUQTXXXqJDLSDPNQIPUPTEBSXJOCFMM
Slide 20
Slide 20 text
ೖΕࢠߏͷಛ ΞϓϦຊମΛแΉΑ͏ʹ࣮ ֎ଆ͔Β৽͍͠;Δ·͍ΛՃͰ͖Δ ࢀߟ"01ʢΞεϖΫτࢦϓϩάϥϛϯάʣ ڞ௨ͷॲཧΛԣஅతʹݺͼग़͠ લॲཧɾޙॲཧΛࢦఆͰ͖Δ
Slide 21
Slide 21 text
XFCΞϓϦʹద༻
Slide 22
Slide 22 text
1)1ʹ͓͚Δྺ࢙
Slide 23
Slide 23 text
4UBDL1)1 IUUQTTUBDLQIQDPN l$PNQPTJOH)UUQ,FSOFM*OUFSGBDF NJEEMFXBSFTTJODFz l8IBUEPXFXBOU 3FVTFBUUIF XFCMBZFSz 4ZNGPOZͷ)UUQ,FSOFM*OUFSGBDF
Slide 24
Slide 24 text
4UBDL1)1
Slide 25
Slide 25 text
143ͱʁ IUUQTXXXQIQpHPSHQTS l1)14UBOEBSET3FDPNNFOEBUJPOTz ܾఆతͳʮඪ४ʯͰͳ͍ ͋͘·ͰΦεεϝɺקࠂ ίϛϡχςΟʔͰӨڹྗେ
Slide 26
Slide 26 text
Α͘ฉ͘143 143#BTJD$PEJOH4UBOEBSE 143$PEJOH4UZMF(VJEF 143-PHHFS*OUFSGBDF 143"VUPMPBEJOH4UBOEBSE
Slide 27
Slide 27 text
143 IUUQTXXXQIQpHPSHQTSQTS )551NFTTBHFJOUFSGBDFT 1TSa)UUQa.FTTBHFa3FRVFTU*OUFSGBDF 1TSa)UUQa.FTTBHFa3FTQPOTF*OUFSGBDF XFCΞϓϦͷೖग़ྗͷඪ४Խ
Slide 28
Slide 28 text
143 IUUQTXXXQIQpHPSHQTSQTS )5514FSWFS3FRVFTU)BOEMFST 1TSa)UUQa4FSWFSa3FRVFTU)BOEMFS*OUFSGBDF 1TSa)UUQa4FSWFSa.JEEMFXBSF*OUFSGBDF XFCΞϓϦͷඪ४Խ
Slide 29
Slide 29 text
ઌ΄Ͳͷਤ
Slide 30
Slide 30 text
143143
Slide 31
Slide 31 text
.JEEMFXBSF*OUFSGBDF interface MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface; }
Slide 32
Slide 32 text
3FRVFTU)BOEMFS interface RequestHandlerInterface { public function handle( ServerRequestInterface $request ): ResponseInterface; }
Slide 33
Slide 33 text
࠷ۙͷ143ͷ IUUQTUXJUUFSDPNGBCQPUTUBUVT 3FNPWF4ZNGPOZCZGBCQPUu1VMM 3FRVFTUuQIQpHpHTUBOEBSET
Slide 34
Slide 34 text
ϑϨʔϜϫʔΫͱ NJEEMXBSFͷؔ
Slide 35
Slide 35 text
4MJN'SBNFXPSLW IUUQTXXXTMJNGSBNFXPSLDPN )5513PVUFS .JEEMFXBSF 1434VQQPSU %FQFOEFODZ*OKFDUJPO 143
Slide 36
Slide 36 text
4MJN class ExampleMiddleware { public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ): ResponseInterface { return $next($request, $response); } }
Slide 37
Slide 37 text
4MJN'SBNFXPSLW IUUQTHJUIVCDPNTMJNQIQ4MJN CMPCY$)"/(&-0(NE 4MJNOPMPOHFSTIJQTXJUIB143 JNQMFNFOUBUJPO 1433FRVFTU)BOEMFSTDBOOPX CFVTFEBTSPVUFDBMMBCMFT
Slide 38
Slide 38 text
;FOE&YQSFTTJWF IUUQTEPDT[FOEGSBNFXPSLDPN [FOEFYQSFTTJWF 1431433PVUJOH143 ඪ४Λҙࣝ
Slide 39
Slide 39 text
-BSBWFM IUUQTMBSBWFMDPNEPDT NJEEMFXBSF 143ͷ࣮Ͱͳ͍ ैདྷͷDPOUSPMMFSͷิॿతͳׂ ڞ௨ॲཧΛ࣮͢ΔͨΊͷΈ
Slide 40
Slide 40 text
-BSBWFM class ExampleMiddleware { public function handle( \Illuminate\Http\Request $request, \Closure $next ) { return $next($request); } }
Slide 41
Slide 41 text
4ZNGPOZ NJEEMFXBSFʹ૬͢Δػೳͳ͍ ػೳͱͯ͠ϑΟϧλʔ͕͋Δ IUUQTTZNGPOZDPNEPDDVSSFOU FWFOU@EJTQBUDIFS CFGPSF@BGUFS@pMUFSTIUNM
Slide 42
Slide 42 text
$BLF1)1 IUUQTCPPLDBLFQIQPSHFO DPOUSPMMFSTNJEEMFXBSFIUNM ͔Β143ʹରԠ $43'τʔΫϯνΣοΫͷ࣮ͳͲ͕ NJEEMFXBSFͰͳ͞Ε͍ͯΔ
Slide 43
Slide 43 text
$BLF1)1 class ExampleMiddleware { public function handle( ServerRequestInterface $request, ResponseInterface $response, callable $next ): \Cake\Http\Response { return $next($request, $response); } }
Slide 44
Slide 44 text
ͯ͞ɺ·ͱΊ
Slide 45
Slide 45 text
ͪΐͬͱେ͛͞ͳ
Slide 46
Slide 46 text
ͦͦϑϨʔϜϫʔΫͱ ϦΫΤετΛड͚औΔ SPVUJOHΛݩʹߦ͏ॲཧΛબ͢Δ ॲཧΛݺͼग़͠ɺ࣮ߦ ݁ՌΛϨεϙϯεͱͯ͠ग़ྗ͢Δ
Slide 47
Slide 47 text
ϑϨʔϜϫʔΫ ࡞Εͦ͏͡Όͳ͍Ͱ͔͢ʁ
Slide 48
Slide 48 text
ϑϨʔϜϫʔΫϨγϐ 143ͷ࣮ʢ[FOEEJBDUPSPTͳͲʣ SPVUJOH࣮ʢ'BTU3PVUFͳͲʣ NJEEMFXBSFͷ࣮ߦʢ.JEEMFMBOEͳͲʣ ग़ྗʢ[FOEIUUQIBOEMFSSVOOFSͳͲʣ
Slide 49
Slide 49 text
࣮ྫ IUUQTMPHNJKQUFDIBSUJDMFT ϩάϛʔͷϦχϡʔΞϧ lࠓճͷϦχϡʔΞϧͰ͜ͷ143 Λར༻͢Δ͜ͱͰɺಛఆͷ8FCϑϨʔ ϜϫʔΫΛར༻ͤͣʹΞϓϦΛ࣮͢ Δ͜ͱʹ͠·ͨ͠z
Slide 50
Slide 50 text
ΦϨΦϨϑϨʔϜϫʔΫ ϑϨʔϜϫʔΫ؆୯ʹ࡞ΕΔ 143ʹ४ڌ͓͚ͯ͠ɺޙ͔Β;FOE &YQSFTTJWFͳͲʹΓ͑Δ͜ͱՄೳ ͪΖΜɺٯ બࢶͷҰͭͱͯ͠ɺ͋Γ͔
Slide 51
Slide 51 text
NJEEMFXBSF ͍͔͕Ͱ͔ͨ͠ʁ
Slide 52
Slide 52 text
·ͱΊ ॲཧΛNJEEMFXBSFͰׂ هड़͕؆ܿʹɺςετ༰қʹ 143Ͱඪ४Խ͞ΕΔ͜ͱͰࠩ͠ସ͑Մೳʹ %*ίϯςφͱͷΈ߹Θͤศར ίϯετϥΫλΠϯδΣΫγϣϯ
Slide 53
Slide 53 text
͋Γ͕ͱ͏͍͟͝·ͨ͠