Slim Frameworkで始めるPHPのmiddleware

Slim Frameworkで始めるPHPのmiddleware

2017年7月17日、PHPカンファレンス関西2017での発表資料です。
https://2017.kphpug.jp/

325afd2c9db540c83485f509f2845acb?s=128

Hiroyuki Yamaoka

July 15, 2017
Tweet

Transcript

  1. 4MJN'SBNFXPSLͰ࢝ΊΔ 
 1)1ͷNJEEMFXBSF 1)1ΧϯϑΝϨϯεؔ੢ 
 ߹ಉձࣾςϯϚυɹࢁԬ޿޾

  2. ࣗݾ঺հ  ࢁԬ޿޾!IJSP@Z  1)1ͱ͔/PEFKTͱ͔ॻ͖·͢  Ұ൪޷͖ͳͷ͸$44  ߹ಉձࣾςϯϚυ୅දࣾһ

  3. ߹ಉձࣾςϯϚυ  ೥݄ઃཱʢظ໨ʣ   ୅දࣾһਓ!!  Ϗδωεࢧԉࣄۀ  αʔϏε։ൃࣄۀ

     JSVDBNJNFNPDPOBTV
  4. NJEEMFXBSFͱ͸ʁ ͱ͍͏࿩Λ͠·͢

  5. XFCΞϓϦέʔγϣϯͱ͸  )551ͷϦΫΤετΛड͚औΓ  ͍Ζ͍ΖॲཧΛͯ͠  ೝূͱ͔%#ʹσʔλอଘͱ͔  ͍ΘΏΔϏδωεϩδοΫ 

    )551ͷϨεϙϯεΛฦ͢
  6. ؆୯ʹඳ͘ͱ͜͏

  7. ϑΟϧλʔͱ͍͏ൃ૝  શϦΫΤετڞ௨ͷॲཧ  ॲཧલͷϩάΠϯೝূ  ॲཧલͷσʔλऔಘɹͳͲ  ΞϓϦέʔγϣϯຊମͷલʹ
 ॲཧΛڬΊΔΑ͏ʹ͢Ε͹Α͍ͷͰ͸

  8. ϑΟϧλʔʢલॲཧʣ

  9. ϑΟϧλʔͷൃ૝ɺଓ͖  શϦΫΤετڞ௨ͷॲཧɺ·ͩ͋Δ  ॲཧޙͷϩΪϯά  ॲཧޙʹ·ͱΊͯϝʔϧૹ৴  ΞϓϦέʔγϣϯຊମͷޙʹ΋
 ॲཧΛڬΊΔΑ͏ʹ͢Ε͹͍͍ΑͶ

  10. ϑΟϧλʔʢޙॲཧʣ

  11. ͜͜Ͱ/PEFKTͷ &YQSFTTͷ࿩Λ͠·͢

  12. &YQSFTT  IUUQFYQSFTTKTDPN  l'BTU VOPQJOJPOBUFE  NJOJNBMJTUXFCGSBNFXPSLGPS /PEFKTz 

    /PEFKTͰҰ൪࢖ΘΕ͍ͯΔ
 XFCΞϓϦέʔγϣϯϑϨʔϜϫʔΫ
  13. )FMMP8PSME const express = require('express'); const app = express(); app.get('/',

    function(req, res) { res.send('Hello World!'); }); app.listen(3000);
  14. ͖ͬ͞ͷਤ

  15. &YQSFTTͷߏ੒  SPVUJOH  ϦΫΤετΛॲཧʹϚοϐϯά  NJEEMFXBSF  SPVUJOHͷաఔͰ
 ೚ҙͷॲཧΛࠩ͠ࠐΉͨΊͷ࢓૊Έ

  16. &YQSFTTͷNJEEMFXBSF  ࢖͍Ͳ͜Ζ  ڞ௨ॲཧʢલॲཧɺޙॲཧʣ  ͭͷSPVUJOHʹରͯ͠ෳ਺࿈݁Ͱ͖Δ  OFYUDBMMCBDLΛݺͿͱ
 ࣍ͷNJEEMFXBSFʹॲཧ͕ҠΔ

     ࣮ߦͷॱং͸એݴॱ
  17. NJEEMFXBSFͷجຊ function(req, res, next) { // Կ͔લॲཧΛ͢Δ next(); // ࣍ͷmiddlewareΛݺͿ

    // Կ͔ޙॲཧΛ͢Δ };
  18. NJEEMFXBSFͷྫ const myLogger = function(req, res, next) { console.log('Logged!'); next();

    }; app.get('/', myLogger, function(req, res) { res.send('Hello World!'); });
  19. NJEEMFXBSFͷྫ શ const myLogger = function(req, res, next) { console.log('Logged!');

    next(); }; app.use(myLogger); // શϦΫΤετʹద༻ app.get('/', function(req, res) { res.send('Hello World!'); });
  20. ೖΕࢠʹͳ͍ͬͯΔ

  21. NJEEMFXBSFͷྫʢෳ਺ʣ const auth = function(req, res, next) {}; const logger

    = function(req, res, next) {}; app.get('/', auth, logger, function(req, res) { res.send('Hello World!'); });
  22. NJEEMFXBSFͷྫʢʁʣ const myLogger = function(req, res) { console.log('Logged!'); }; app.get('/',

    function(req, res, next) { res.send('Hello World!'); next(); }, myLogger); // ॲཧ࣮ߦޙʹݺ͹ΕΔ
  23. ͋ΒΏΔॲཧ͕ NJEEMFXBSFͱͯ͠ ࣮૷ɾ࣮ߦ͞ΕΔੈք

  24. ೖΕࢠঢ়ଶ

  25. λϚωΪΈ͍ͨʁ QIPUPCZEBSXJO#FMMMJDFOTFEVOEFS$$#:  
 IUUQTXXXqJDLSDPNQIPUPTEBSXJOCFMM

  26. Ұํ1)1Ͱ͸ ʢؼ͖ͬͯͨʂʣ

  27. 4UBDL1)1  IUUQTUBDLQIQDPN  l$PNQPTJOH)UUQ,FSOFM*OUFSGBDF NJEEMFXBSFTTJODFz  l8IBUEPXFXBOU 3FVTFBUUIF XFCMBZFSz

     4ZNGPOZͷ)UUQ,FSOFM*OUFSGBDF
  28. 4UBDL1)1

  29. Ͱ͸ͦΖͦΖ 4MJN'SBNFXPSLͷ࿩

  30. 4MJN'SBNFXPSLW  IUUQTXXXTMJNGSBNFXPSLDPN  )5513PVUFS  .JEEMFXBSF  1434VQQPSU 

    %FQFOEFODZ*OKFDUJPO
  31. 143  IUUQXXXQIQpHPSHQTSQTS  )551NFTTBHFJOUFSGBDFT  1TSa)UUQa.FTTBHFa3FRVFTU*OUFSGBDF  1TSa)UUQa.FTTBHFa3FTQPOTF*OUFSGBDF 

    XFCΞϓϦͷೖग़ྗͷඪ४Խ
  32. )FMMP8PSME // autoloadͱ͔use͸লུ… $app = new \Slim\App(); $app->get('/', function($request, $response)

    { $response->getBody() ->write('Hello World!'); return $response; }); $app->run();
  33. ؆୯Ͱ͠ΐʁ

  34. ͍Α͍Α NJEEMFXBSFͷ࿩

  35. NJEEMFXBSFʢؔ਺ʣ function(Request $request, Response $response, callable $next): Response {
 //

    Կ͔લॲཧΛ͢Δ $response = $next($request, $response); // Կ͔ޙॲཧΛ͢Δ return $response; };
  36. NJEEMFXBSFʢΫϥεʣ class ExampleMiddleware { public function __invoke( Request $request, Response

    $response, callable $next ): Response { // …ؔ਺ͷͱ͖ͱಉ͡ } }
  37. NJEEMFXBSFͷద༻ $app->get('/', function($request, $response) { // Կ͔ॲཧͯ͠ResponseΛฦ͢ })->add(ExampleMiddleware::class); // ΋͠΋શॲཧʹڬΉͳΒ

    $app->add(ExampleMiddleware::class);
  38. DPOUSPMMFS // $app->get('/', ExampleAction::class); class ExampleAction { public function __invoke(

    Request $request, Response $response, array $args): Response
 { return $response; } }
  39. %*ίϯςφͱ ૊Έ߹ΘͤΔͱ ΋ͬͱศརʹ

  40. 4MJNͷ%*ίϯςφͷҐஔ෇͚  Πϯελϯεੜ੒͸%*ίϯςφͷ੹຿  ޷͖ͳ%*ίϯςφʹࠩ͠ସ͑Մೳ  1JNQMF1)1%*"DDMJNBUF  $POUBJOFS*OUFSPQ࣮૷ͷίϯςφ 

    143࣮૷ͷίϯςφʢ͔Βʣ
  41. %*ίϯςφͱͷ૬ੑ  @@JOWPLFϝιου͕͋Ε͹Α͍ͷͰ  DPOUSPMMFS΋NJEEMFXBSF΋  ίϯετϥΫλΠϯδΣΫγϣϯ  ೚ҙͷΠϯελϯεΛ౉ͤΔ 

    NPEFMΫϥεͱ͔ɺSFOEFSFSͱ͔
  42. DPOUSPMMFS /** @var \Slim\Views\Twig */ private $view; public fucntion __construct(Twig

    $view) { $this->view = $view; } public function __invoke(Request $request, Response $response, array $args): Response { return $this->view->render($response, 'foo.twig'); }
  43. ࿩Λ໭ͯ͠ɺ143ͷ࿩

  44. ؔ܎͋Γͦ͏ͳ143  IUUQXXXQIQpHPSHQTS  1)14UBOEBSET3FDPNNFOEBUJPOT  143)551.FTTBHF*OUFSGBDF"DDFQUFE   143$POUBJOFS*OUFSGBDF"DDFQUFE

      143)551.JEEMFXBSFT%SBGU
  45. 143 "DDFQUFE  %*ίϯςφͷJOUFSGBDFͷඪ४Խ  1TSa$POUBJOFSa$POUBJOFS*OUFSGBDF  HFUͱIBTͷϝιουΛ࣋ͭ

  46. 143 %SBGU  .JEEMFXBSF*OUFSGBDF  NJEEMFXBSFࣗମʹؔ͢Δ΋ͷ  %FMFHBUF*OUFSGBDF  ҕ೚ઌίΞͷΞϓϦϨΠϠʔʁ

     ·ͩٞ࿦ͷߦํ͕ఆ·͍ͬͯͳ͍
  47. 1)1ͷNJEEMFXBSFྲّྀ  %PVCMF1BTT  4MJN͸ͬͪ͜  GO SFRVFTU SFTQPOTF OFYU

    SFTQPOTF  4JOHMF1BTT  GO SFRVFTU OFYU SFTQPOTF
  48. .JEEMFXBSF*OUFSGBDF namespace Psr\Http\ServerMiddleware; interface MiddlewareInterface { public function process( ServerRequestInterface

    $request, DelegateInterface $delegate ): ResponseInterface; }
  49. %FMFHBUF*OUFSGBDF namespace Psr\Http\ServerMiddleware; interface DelegateInterface { public function process( ServerRequestInterface

    $request ): ResponseInterface; }
  50. ଞͷϑϨʔϜϫʔΫͷ৔߹

  51. -BSBWFM  IUUQTMBSBWFMDPNEPDT NJEEMFXBSF  143ͳͲ͸ҙ͍ࣝͯ͠ͳ͍  ैདྷͷDPOUSPMMFSͷิॿతͳ໾ׂ  ڞ௨ॲཧΛ࣮૷͢ΔͨΊͷ࢓૊Έ

  52. -BSBWFMྫ class CheckAge { public function handle( \Illuminate\Http\Request $request, \Closure

    $next) { // Կ͔ॲཧʢલͰ΋ޙΖͰ΋ʣ return $next($request); } } Route::get('/', function() { /* ॲཧ */ }) ->middleware(CheckAge::class);
  53. $BLF1)1  $BLF1)1͔ΒNJEEMFXBSF͕͋Δ  ࣮૷ํ๏͸4MJNͱ΄ͱΜͲಉ͕ͩ͡ɺ
 ਖ਼௚͜ͳΕ͍ͯͳ͍ײ͡  ΫϥεͰ͸ͳ͘ΠϯελϯεΛ౉͢  IUUQTXXXTMJEFTIBSFOFUNBSLTUPSZ

    DBLFQIQUIFSPBEBIFBE
  54. 4ZNGPOZ  NJEEMFXBSFʹ૬౰͢Δػೳ͸ͳ͍  ػೳͱͯ͠͸ϑΟϧλʔ͕͋Δ  4JMFYͰNJEEMFXBSFͱݺͿ΋ͷ͸͋Δ  ͕ɺ͍ΘΏΔϑΟϧλʔͰ͔͠ͳ͍

  55. ;FOE&YQSFTTJWF  IUUQTEPDT[FOEGSBNFXPSLDPN [FOEFYQSFTTJWF  143.JEEMFXBSF3PVUJOH%*  143΍ͳͲͷඪ४Λҙࣝ  BDUJPOΫϥε΋NJEEMFXBSFͱ࣮ͯ͠૷

  56. ;FOE&YQSFTTJWFྫ $helloMiddleware = function ( ServerRequestInterface $request, DelegateInterface $delegate )

    { return new TextResponse('Hello', 200); }; // ͋Δ͍͸MiddlewareInterfaceΛ࣮૷ͨ͠Ϋϥε $app->get('/hello', $hellosMiddleware);
  57. NJEEMFXBSF ͍͔͕Ͱ͔ͨ͠ʁ

  58. ·ͱΊ  NJEEMFXBSFͱ͍͏ߟ͑ํΛ஌Δ͜ͱ  ॲཧΛNJEEMFXBSFͰ෼ׂ➡ςετ༰қੑ  143Ͱඪ४Խ͞ΕΔ͜ͱͰࠩ͠ସ͑Մೳʹ  %*ίϯςφͱͷ૊Έ߹Θͤɺศར 

    4MJNͱ͔;FOE&YQSFTTJWF໘ന͍Αʂ
  59. ͋Γ͕ͱ͏͍͟͝·ͨ͠ ࣭໰͕͋Ε͹Ͳ͏ͧ