Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPのmiddlewareを 使いこなすために
Search
YAMAOKA Hiroyuki
January 26, 2019
Technology
3
2.4k
PHPのmiddlewareを 使いこなすために
PHPカンファレンス仙台2019での発表資料です。
https://phpcon-sendai.net/2019/
YAMAOKA Hiroyuki
January 26, 2019
Tweet
Share
More Decks by YAMAOKA Hiroyuki
See All by YAMAOKA Hiroyuki
エンジニアの「センス」とは何か / What is the sense of engineers
hiro_y
19
8.7k
CSRF対策のやり方、そろそろアップデートしませんか / Update your knowledge of CSRF protection
hiro_y
29
24k
PHPで任意精度演算を行って「正しい」金額計算をする方法 / Perform arbitrary precision arithmetic in PHP to achieve "accurate" monetary calculations
hiro_y
2
2.8k
PHPのバージョンアップ実際のところどうなの? / How actually upgrade of PHP is
hiro_y
3
730
PHPのDI、attributesとこれから / PHP DI with attributes
hiro_y
1
2.2k
PHPのアノテーションの仕組みとメリット・デメリット / About PHP annotations
hiro_y
1
8.3k
株式会社 USEN Media - PHPカンファレンス北海道2019 / 2019-09-21_phpcondo-2019_usen-media
hiro_y
0
210
Slim Frameworkで始めるPHPのmiddleware
hiro_y
4
2.1k
Node.jsやPHPでも こわくないHeroku
hiro_y
1
1.6k
Other Decks in Technology
See All in Technology
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
150
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
250
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
260
Postman と API セキュリティ / Postman and API Security
yokawasa
0
200
Wantedly での Datadog 活用事例
bgpat
1
440
生成AIのガバナンスの全体像と現実解
fnifni
1
190
2024年にチャレンジしたことを振り返るぞ
mitchan
0
140
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
UI State設計とテスト方針
rmakiyama
2
580
.NET 9 のパフォーマンス改善
nenonaninu
0
900
大幅アップデートされたRagas v0.2をキャッチアップ
os1ma
2
530
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Code Review Best Practice
trishagee
65
17k
Site-Speed That Sticks
csswizardry
2
190
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Building Your Own Lightsaber
phodgson
103
6.1k
Done Done
chrislema
181
16k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Transcript
1)1ͷNJEEMFXBSFΛ ͍͜ͳͨ͢Ίʹ 1)1ΧϯϑΝϨϯεઋ ߹ಉձࣾςϯϚυɹࢁԬ
ࣗݾհ ࢁԬ!IJSP@Z 1)1ͱ͔/PEFKTͷΤϯδχΞ 1)1ͷ͜Ζ͔Β ߹ಉձࣾςϯϚυදࣾһ
ɹɹ߹ಉձࣾςϯϚυ!! Ϗδωεࢧԉࣄۀ ࣾ֎$50ͱ͔ΞυόΠβʔ *OOPWBUPS+BQBO64&/.FEJBͳͲ αʔϏε։ൃࣄۀ
JSVDBNJNFNPDPOBTV
ࠓճͷ͓ͷΰʔϧ NJEEMFXBSFͱ͍͏ߟ͑ํ͕Θ͔Δ ฉ͍ͨͱ͖ʹΠϝʔδ͕ු͔Ϳ ར༻γʔϯ͕૾Ͱ͖Δ ϑϨʔϜϫʔΫͰ͑Δ͔Θ͔Δ
͓Λ࢝ΊΔલʹ
લճ·Ͱͷ͋Β͢͡ 1)1ΧϯϑΝϨϯεؔ 4MJN'SBNFXPSLͰ࢝ΊΔ1)1ͷ NJEEMFXBSF 1)1ΧϯϑΝϨϯε 1)1'*(ͷ)551ॲཧඪ४ͷઃܭ
ͳͥ143ʹͳͬͨͷ͔
Ͱɺ͋ΒͨΊͯ
XFCΞϓϦͱ ϦΫΤετΛड͚औΓʢೖྗʣ ͍Ζ͍ΖॲཧΛͯ͠ ೝূͱ͔%#ͷॲཧͱ͔ ϨεϙϯεΛฦ͢ʢग़ྗʣ
؆୯ʹඳ͘ͱ͜͏
XFCΞϓϦͷલॲཧ શϦΫΤετڞ௨Ͱ͍ͨ͠ॲཧ ϩάΠϯೝূ %#͔ΒͷσʔλऔಘɹͳͲ ΞϓϦέʔγϣϯຊମͷલʹ ॲཧΛڬΊΔΑ͏ʹ͢ΕΑ͍ͷͰ
ϑΟϧλʔʢલॲཧʣ
XFCΞϓϦͷޙॲཧ શϦΫΤετڞ௨ͷॲཧɺ·ͩ͋Δ ϩάग़ྗ ॲཧޙʹ·ͱΊͯϝʔϧૹ৴ ΞϓϦέʔγϣϯຊମͷޙʹ ॲཧΛڬΊΔΑ͏ʹͰ͖ΔͱΑͦ͞͏
ϑΟϧλʔʢޙॲཧʣ
ͯ͞ɺͪΐͬͱ ൃΛస͢Δ
ೖΕࢠߏ ೖΕࢠࣜʹॲཧΛ ͭͳ͍Ͱ͍͘ΈΛߟ͑Δ ྫؔ"ͱؔ#Λ࣮͢Δ ؔ"ͦͷॲཧͷதͰؔ#Λ ࣮ߦɺͦͷ݁ՌΛฦ͢Α͏ʹ͢Δ
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
ೖΕࢠߏ
͞ΒʹೖΕࢠߏ
λϚωΪΈ͍ͨʁ QIPUPCZEBSXJO#FMMMJDFOTFEVOEFS$$#: IUUQTXXXqJDLSDPNQIPUPTEBSXJOCFMM
ೖΕࢠߏͷಛ ΞϓϦຊମΛแΉΑ͏ʹ࣮ ֎ଆ͔Β৽͍͠;Δ·͍ΛՃͰ͖Δ ࢀߟ"01ʢΞεϖΫτࢦϓϩάϥϛϯάʣ ڞ௨ͷॲཧΛԣஅతʹݺͼग़͠
લॲཧɾޙॲཧΛࢦఆͰ͖Δ
XFCΞϓϦʹద༻
1)1ʹ͓͚Δྺ࢙
4UBDL1)1 IUUQTTUBDLQIQDPN l$PNQPTJOH)UUQ,FSOFM*OUFSGBDF NJEEMFXBSFTTJODFz l8IBUEPXFXBOU 3FVTFBUUIF XFCMBZFSz
4ZNGPOZͷ)UUQ,FSOFM*OUFSGBDF
4UBDL1)1
143ͱʁ IUUQTXXXQIQpHPSHQTS l1)14UBOEBSET3FDPNNFOEBUJPOTz ܾఆతͳʮඪ४ʯͰͳ͍ ͋͘·ͰΦεεϝɺקࠂ
ίϛϡχςΟʔͰӨڹྗେ
Α͘ฉ͘143 143#BTJD$PEJOH4UBOEBSE 143$PEJOH4UZMF(VJEF 143-PHHFS*OUFSGBDF 143"VUPMPBEJOH4UBOEBSE
143 IUUQTXXXQIQpHPSHQTSQTS )551NFTTBHFJOUFSGBDFT 1TSa)UUQa.FTTBHFa3FRVFTU*OUFSGBDF 1TSa)UUQa.FTTBHFa3FTQPOTF*OUFSGBDF
XFCΞϓϦͷೖग़ྗͷඪ४Խ
143 IUUQTXXXQIQpHPSHQTSQTS )5514FSWFS3FRVFTU)BOEMFST 1TSa)UUQa4FSWFSa3FRVFTU)BOEMFS*OUFSGBDF 1TSa)UUQa4FSWFSa.JEEMFXBSF*OUFSGBDF
XFCΞϓϦͷඪ४Խ
ઌ΄Ͳͷਤ
143143
.JEEMFXBSF*OUFSGBDF interface MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface
$handler ): ResponseInterface; }
3FRVFTU)BOEMFS interface RequestHandlerInterface { public function handle( ServerRequestInterface $request ):
ResponseInterface; }
࠷ۙͷ143ͷ IUUQTUXJUUFSDPNGBCQPUTUBUVT 3FNPWF4ZNGPOZCZGBCQPUu1VMM 3FRVFTUuQIQpHpHTUBOEBSET
ϑϨʔϜϫʔΫͱ NJEEMXBSFͷؔ
4MJN'SBNFXPSLW IUUQTXXXTMJNGSBNFXPSLDPN )5513PVUFS .JEEMFXBSF 1434VQQPSU
%FQFOEFODZ*OKFDUJPO 143
4MJN class ExampleMiddleware { public function __invoke( ServerRequestInterface $request,
ResponseInterface $response, callable $next ): ResponseInterface { return $next($request, $response); } }
4MJN'SBNFXPSLW IUUQTHJUIVCDPNTMJNQIQ4MJN CMPCY$)"/(&-0(NE 4MJNOPMPOHFSTIJQTXJUIB143 JNQMFNFOUBUJPO 1433FRVFTU)BOEMFSTDBOOPX CFVTFEBTSPVUFDBMMBCMFT
;FOE&YQSFTTJWF IUUQTEPDT[FOEGSBNFXPSLDPN [FOEFYQSFTTJWF 1431433PVUJOH143 ඪ४Λҙࣝ
-BSBWFM IUUQTMBSBWFMDPNEPDT NJEEMFXBSF 143ͷ࣮Ͱͳ͍ ैདྷͷDPOUSPMMFSͷิॿతͳׂ ڞ௨ॲཧΛ࣮͢ΔͨΊͷΈ
-BSBWFM class ExampleMiddleware { public function handle( \Illuminate\Http\Request $request,
\Closure $next ) { return $next($request); } }
4ZNGPOZ NJEEMFXBSFʹ૬͢Δػೳͳ͍ ػೳͱͯ͠ϑΟϧλʔ͕͋Δ IUUQTTZNGPOZDPNEPDDVSSFOU FWFOU@EJTQBUDIFS CFGPSF@BGUFS@pMUFSTIUNM
$BLF1)1 IUUQTCPPLDBLFQIQPSHFO DPOUSPMMFSTNJEEMFXBSFIUNM ͔Β143ʹରԠ $43'τʔΫϯνΣοΫͷ࣮ͳͲ͕ NJEEMFXBSFͰͳ͞Ε͍ͯΔ
$BLF1)1 class ExampleMiddleware { public function handle( ServerRequestInterface $request,
ResponseInterface $response, callable $next ): \Cake\Http\Response { return $next($request, $response); } }
ͯ͞ɺ·ͱΊ
ͪΐͬͱେ͛͞ͳ
ͦͦϑϨʔϜϫʔΫͱ ϦΫΤετΛड͚औΔ SPVUJOHΛݩʹߦ͏ॲཧΛબ͢Δ ॲཧΛݺͼग़͠ɺ࣮ߦ ݁ՌΛϨεϙϯεͱͯ͠ग़ྗ͢Δ
ϑϨʔϜϫʔΫ ࡞Εͦ͏͡Όͳ͍Ͱ͔͢ʁ
ϑϨʔϜϫʔΫϨγϐ 143ͷ࣮ʢ[FOEEJBDUPSPTͳͲʣ SPVUJOH࣮ʢ'BTU3PVUFͳͲʣ NJEEMFXBSFͷ࣮ߦʢ.JEEMFMBOEͳͲʣ ग़ྗʢ[FOEIUUQIBOEMFSSVOOFSͳͲʣ
࣮ྫ IUUQTMPHNJKQUFDIBSUJDMFT ϩάϛʔͷϦχϡʔΞϧ lࠓճͷϦχϡʔΞϧͰ͜ͷ143 Λར༻͢Δ͜ͱͰɺಛఆͷ8FCϑϨʔ ϜϫʔΫΛར༻ͤͣʹΞϓϦΛ࣮͢ Δ͜ͱʹ͠·ͨ͠z
ΦϨΦϨϑϨʔϜϫʔΫ ϑϨʔϜϫʔΫ؆୯ʹ࡞ΕΔ 143ʹ४ڌ͓͚ͯ͠ɺޙ͔Β;FOE &YQSFTTJWFͳͲʹΓ͑Δ͜ͱՄೳ ͪΖΜɺٯ બࢶͷҰͭͱͯ͠ɺ͋Γ͔
NJEEMFXBSF ͍͔͕Ͱ͔ͨ͠ʁ
·ͱΊ ॲཧΛNJEEMFXBSFͰׂ هड़͕؆ܿʹɺςετ༰қʹ 143Ͱඪ४Խ͞ΕΔ͜ͱͰࠩ͠ସ͑Մೳʹ %*ίϯςφͱͷΈ߹Θͤศར
ίϯετϥΫλΠϯδΣΫγϣϯ
͋Γ͕ͱ͏͍͟͝·ͨ͠