Slim docsã®è§£æ; Middleware
https://www.slimframework.com/docs/v3/concepts/middleware.html
ãã¿çã«ã¯ãæéè¦ã¯ã©ã¹ãã«éè¦ãªãã¿ãªã®ã§ãè
°ãæ®ãã¦ã
ã¨ããããããã¡ãã®ç»åãä¸çªãããããããã ãããªããã£ã¦æãã
https://www.slimframework.com/docs/v3/images/middleware.png
ä»ã¾ã§æ¸ãã¦ããã®ã¯appãã¤ã¾ãã¯ãä¸çªçãä¸ãã§ã
ãã®å¤å´ã«ãMiddleware 1ããã©ããã³ã°ãã¦ãããã«ãã®å¤å´ã«ãMiddleware 2ããã©ããã³ã°ãã¦â¦â¦çã«ãçããã®ãããªç¶æ
ã«ããæãã
ãªã®ã§ãå®è¡é çªã¨ãã¦ã¯
ã»Middleware 2ã®å
¥ãå£
ã»Middleware 1ã®å
¥ãå£
ã»app(æ¬ä½)
ã»Middleware 1ã®åºå£
ã»Middleware 2ã®åºå£
ã£ã¦æãã§åããã¨ã³ã¸ãã¢ãªãç解ããããå
¥ãåæ§é ãªããããªãããªãï¼
å人çã«ã¯(ï¼MagicWeaponã§ã¯)ããã®è¾ºããã¯ã©ã¹ã®ç¶æ¿ãé¢ä¿ã§æ¸ãã¦ããã ãã©ã
ãããã¯ã©ã¹ã®ç¶æ¿ã§æ¸ãã°ããããããã£ã¦æããªãããªããã ãã©(åé¡ãªãæ¸ãã¦ãã)ããã ã¾ããå¾ã§ä»ã足ããã£ã¦æãã§èããã¨ããã®Middlewareã£ã¦èãæ¹ã¯ãé¢ç½ããªããã£ã¦æããã¾ãã®ã
ãªãã¦æãã§ã大ã¾ãã«æ¦å¿µãææ¡ãããã¨ããã§ããã£ãããå®è£
æ¹æ³ã
åºæ¬çã«ã¯
ã»Middlewareæ¬ä½ã®ã¯ã©ã¹ã®è¨è¿°
ã»Middlewareããé©ç¨ããããããã®ãrouterã¸ã®ç»é²
ã®ï¼ç¨®é¡ã®æé ãå¿
è¦ã½ã
Middlewareã¯ã©ããå®éã«ã¯ãã¯ã©ã¹ã§ãé¢æ°ã§ãOKãã½ããã ãã©ãã¾ãã¨ããããã¯ã©ã¹ã
ãã®å ´åãå®è£
ã¯__invoke()ã«æ¸ãã¦ããã£ã¦æããããã
ãããå¤åãMiddlewareãå
¥ããå ´æãç¨æãããï¼ããã®è¾ºãauto_loaderã§èªã¿è¾¼ããããã«ãããçãªãã¨ãå¿
è¦ãªãã ãããªãã
ãã ãã®è¾ºã®èå¯ã¯å³å¯ã«ã¯ãä»åã®èå¯ã®ç¯çå¤ããªã®ã§ãä»åã¯ãindex.phpã«ã¹ãæ¸ããã¨ããããä¹±éãªæ¹æ³ã使ãã¾ãï½
class ExampleMiddleware { public function __invoke($request, $response, $next) { $response->write('BEFORE:'); $response = $next($request, $response); $response->write(':AFTER'); return $response; } } $app = new \Slim\App; $app->add(new ExampleMiddleware() ); $app->get('/', function(Request $request, Response $response, array $args) { echo "hoge test\n"; }); $app->get('/aaa', function(Request $request, Response $response, array $args) { echo "hoge aaa\n"; });
BEFORE::AFTERhoge test
ãããâ¦â¦â¦ããããããechoã§åã£ã¦ãã¦ãããããæå¾ã«è¶³ããã¡ããã®ãã
ã¡ããã¨æ¸ãæãã
class ExampleMiddleware { public function __invoke($request, $response, $next) { $response->write('BEFORE:'); $response = $next($request, $response); $response->write(':AFTER'); return $response; } } $app = new \Slim\App; $app->add(new ExampleMiddleware() ); $app->get('/', function(Request $request, Response $response, array $args) { return $response->write("hoge test\n"); }); $app->get('/aaa', function(Request $request, Response $response, array $args) { return $response->write("hoge aaaa\n"); });
BEFORE:hoge test
:AFTER
ã¯ãäºæ³ã©ããã
軽ããå ¥ãåã確èªã
class ExampleMiddleware { public function __invoke($request, $response, $next) { $response->write('BEFORE:'); $response = $next($request, $response); $response->write(':AFTER'); return $response; } } class ExampleMiddleware2 { public function __invoke($request, $response, $next) { $response->write('BEFORE 2:'); $response = $next($request, $response); $response->write(':AFTER 2'); return $response; } } $app = new \Slim\App; $app->add(new ExampleMiddleware() ); $app->add(new ExampleMiddleware2() ); $app->get('/', function(Request $request, Response $response, array $args) { return $response->write("hoge test\n"); }); $app->get('/aaa', function(Request $request, Response $response, array $args) { return $response->write("hoge aaaa\n"); });
ä»åº¦ã¯/aaaã«ã¢ã¯ã»ã¹ã
BEFORE 2:BEFORE:hoge aaaa
- AFTER
- AFTER 2
ã¯ãããããã«å ¥ãåã
ãµã¨çåããä¸é¨åºåããä¸é¨å±æãã ã¨ã©ããªããã ãï¼
å¤åãåºåãä¸çªå¤å´ãã ã¨æããããã ãã©ã
class ExampleMiddleware { public function __invoke($request, $response, $next) { $response->write('BEFORE:'); $response = $next($request, $response); $response->write(':AFTER'); return $response; } } class ExampleMiddleware2 { public function __invoke($request, $response, $next) { $response->write('BEFORE 2:'); $response = $next($request, $response); $response->write(':AFTER 2'); return $response; } } $app = new \Slim\App; $app->add(new ExampleMiddleware() ); $app->get('/', function(Request $request, Response $response, array $args) { return $response->write("hoge test\n"); })->add(new ExampleMiddleware2() ); $app->get('/aaa', function(Request $request, Response $response, array $args) { return $response->write("hoge aaaa\n"); });
ã¾ã/aaaã¯
BEFORE:hoge aaaa
:AFTER
ãªã®ã§OKã
次ã/ã«ã¢ã¯ã»ã¹ã
BEFORE:BEFORE 2:hoge test
- AFTER 2
- AFTER
ããäºæ³éãããå ¨ä½âå±æãã§åãããã
Group Middleware
ãããã¾ã§ã¯å¤§ä½
ã»æ³åã§ããåã
ã»ã(ç¡åå«ã)é¢æ°ãã¯åºæ¬æ¸ãã¤ãããªã
ãªã®ã§ãæ¾ç½®ã
Passing variables from middleware
ãããã«ã¦ã§ã¢ããå±æ§ã渡ãæãç°¡åãªæ¹æ³ã¯ãè¦æ±ã®å±æ§ã使ç¨ãããã¨ã§ãã(æ©æ¢°ç¿»è¨³)ãã
$request = $request->withAttribute('foo', 'bar');
ã§å¤ãè¨å®ãã¦
$foo = $request->getAttribute('foo');
ã§ä½¿ããã¨ã
ãµãã
â¦â¦MagicWeaponã®bagã¨ããçªã£è¾¼ãã©ããããããï¼
ã¨ãããããã»ãã®ãè¨æ¶ã¯ãã¦ããã¾ããã
Finding available middleware
ãã
ãè²ã
ããã®ãã¾ããããã
https://github.com/oscarotero/psr7-middlewares
https://github.com/slimphp/Slim/wiki/Middleware-for-Slim-Framework-v3.x
https://github.com/lalop/awesome-psr7
ãã¦ã
ã¡ããã¨æ°ã«ãªã£ãã®ã
ã»middlewareã®ä¸ã§container使ããï¼
ã»ãMiddlewareã®éä¸ã§ã¨ã©ã¼ãã®åºãæ¹
ã®ï¼ã¤ã
ãmiddlewareã®ä¸ã§container使ããï¼ã
ã¯ãã°ã°ã£ã¦ã¿ãããã³ã³ã¹ãã©ã¯ã¿ã§æ¸¡ãããï¼ãã£ã¦ãªè¨è¿°ããã£ãã
ããã¾ãAppã«ç¸å½ããæ¬ä½ããã³ã³ã¹ãã©ã¯ã¿æ¸¡ãããªã®ã§ãããã§ããããªãããã解決ï½
çåã®æ¬å½ãã¨ã©ã¼ãã
äºæ³ãã¦ã¿ãã³ã¼ãã
class ExampleMiddleware { public function __invoke($request, $response, $next) { $response->write('Middleware Error!!'); /* $response->write('BEFORE:'); $response = $next($request, $response); $response->write(':AFTER'); */ return $response; } } class ExampleMiddleware2 { public function __invoke($request, $response, $next) { $response->write('BEFORE 2:'); $response = $next($request, $response); $response->write(':AFTER 2'); return $response; } } $app = new \Slim\App; $app->add(new ExampleMiddleware() ); $app->get('/', function(Request $request, Response $response, array $args) { return $response->write("hoge test\n"); })->add(new ExampleMiddleware2() ); $app->get('/aaa', function(Request $request, Response $response, array $args) { return $response->write("hoge aaaa\n"); });
Middleware Error!!
ãã1mmã®çãããªãäºæ³çä¸ï½
æ¯è¼çãã£ããã¨çµãã£ããªã
ãããã¯Slimãå
¨ä½çã«ææ³ãã·ã³ãã«ã ãç´ æ´ãããã