Slim docsã®è§£æ; Routing
https://www.slimframework.com/docs/v3/objects/router.html
ã¾ãããã¨ãªã大ç©ã
è
°æ®ãã¦ããã¾ãããâ¦â¦ä¸æãããäºåå²ããã
How to create routesã
ãããããã¯ã¾ãOKã
ããã¡ããçã«ã¯
$app = new \Slim\App(); $app->get('/books/{id}', ã¯ã©ã¹å::clss . ':ã¡ã½ããå');
ããæ¸ããªãã¤ããã ãã©(ã«ã¼ãã£ã³ã°ãã¡ã¤ã«ã«å¦çæ¸ãã¨ã以ä¸æ¤é²åé¤ã確èªç¨ã®ã¡ããããã¹ãã¯ä¾å¤)ã
ããä¸å¿ã
ããããSlimèªä½ããPHP5.5+ããªã®ã§ããã¾ãé¢ä¿ãªããã ãã©ã
class ãã¼ã¯ã¼ãã§ã¯ã©ã¹åã®è§£æ±ºãã§ãã( http://php.net/manual/ja/language.oop5.basic.php )ã®ã¯PHP 5.5 以éãªã®ã§ã念ã®ããã«çæã
ãã¨ã¯
$app->any('/books/{id}', ã¯ã©ã¹å::clss . ':ã¡ã½ããå'); $app->map(['GET', 'POST'], '/books/{id}', ã¯ã©ã¹å::clss . ':ã¡ã½ããå');
辺ãã®æ¸ãæ¹ãææ¡ãã¦ããã°ãããªãã
public function any($pattern, $callable) { return $this->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $pattern, $callable); }
ã®å®è£
è¦ãã°ã大ä½ãã³ã¨ããã¨æãã
å人çã«ç´å¾ããã®ããå¥ã®ã¨ããã«æ¸ãã¦ãã£ããã ãã©
$app->map(['GET', 'POST', 'PUT', 'DELETE'], '/', ã¯ã©ã¹å::clss . ':ã¡ã½ããå');
ã£ã¦æ¸ãæ¹ããã確ãã« / ã£ã¦ãã©ã®ã¡ã½ããã§æ¥ã¦ããããã£ã¦ã®ã¯ããããã«ãããªããã¨ã
ã¾ããã®è¾ºã¯ã¹ã¿ã³ã¹ã¨ä¸»ç¾©ä¸»å¼µã¨ã好ã¿ã§ãä»»æã«ã
Route callbacksã
Requestã¨Responseã¯ããããã
Argumentsã¯ã3çªç®ã®å¼æ°ã¯ãç¾å¨ã®ã«ã¼ãã®ååä»ããã¬ã¼ã¹ãã«ãã®å¤ãå«ãé£æ³é
åã§ãã(æ©æ¢°ç¿»è¨³)ãã
ãµãâ¦â¦ã¡ããã¨å¥ä»¶ã§è©¦ããããã¨ãããã®ã§ã試ãã¦ã¿ã¾ããã
ã¾ãã¯åç´ã«ãä¸èº«ã®ç¢ºèªãããã
/hoge/gallu
$app = new \Slim\App; $app->get('/hoge/{name}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; });
array(1) { ["name"]=> string(5) "gallu" }
ã¾ãäºæ³éãã
次ããåæ¹ã«ãã©ã¡ã¿ãã ã¨ãã©ããªããã ãï¼
$app = new \Slim\App; $app->get('/{name}/hoge/{age}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; });
array(2) { ["name"]=> string(5) "gallu" ["age"]=> string(3) "100" }
ããã®ã£ãããµãã¤ã±ã«ã®ãã
次ãã¡ãã£ã¨ã²ãã£ã確èªã
$app = new \Slim\App; $app->get('/{name}/hoge/{age}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; }); $app->get('/admin/hoge/{age}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; });
http://example.com:8080/admin/hoge/100
array(2) { ["name"]=> string(5) "admin" ["age"]=> string(3) "100" }
ãµãã
$app = new \Slim\App; $app->get('/admin/hoge/{age}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; }); $app->get('/{name}/hoge/{age}', function(Request $request, Response $response, array $args) { ob_start(); var_dump($args); $s = ob_get_clean(); $response->getBody()->write($s); return $response; });
http://example.com:8080/admin/hoge/100
array(1) { ["age"]=> string(3) "100" }
ãµããµãã
ã¨ãããããå
åºæå¹ããã¨ã
ãã ããããè¦å®ã®æåã¨ãéããã ããããªããã¡ã¨ãæ¹ççã¯ããã°ããèãã¦ããã¾ãã*1ã
Writing content to the responseã
ãFirst, you can simply echo() content from the route callback.ããµã¸ï¼ï¼ï¼ ããªã®ï¼
$app = new \Slim\App; $app->get('/', function(Request $request, Response $response, array $args) { echo "hoge test\n"; });
ããã»ãã¨ã åããã
ãSecond, you can return a Psr\Http\Message\ResponseInterface object.ãã¯ãããã
ã¨ã¯ããâ¦â¦â¦$response->write()ã¨ãããããobject returnã®ã»ãããå人çã«ã¯ããããããããã«æãããªãã
ã¾ããã¡ãã£ã¨ãããããã°ç¨ã¨ãã§ãè¦ãã¦ããã¾ããã
ããã ã¨ãresponse statusã¨ãå¤ããæãã¨ãã対å¿ãé£ããããªã±ã¼ã¹ãããã ããããã
Closure bindingã
ãã«ã¼ãã³ã¼ã«ããã¯ã¨ãã¦Closureã¤ã³ã¹ã¿ã³ã¹ã使ç¨ããã¨ãã¯ãã¼ã¸ã£ã®ç¶æ
ã¯Containerã¤ã³ã¹ã¿ã³ã¹ã«ãã¤ã³ãããã¾ãã ã¤ã¾ãã$ thisãã¼ã¯ã¼ãã使ç¨ãã¦Closureå
ã®DIã³ã³ããã¤ã³ã¹ã¿ã³ã¹ã«ã¢ã¯ã»ã¹ã§ãã¾ãã(æ©æ¢°ç¿»è¨³)ãã
ãããããã¯ã³ã¼ãã§ãè¦ã¦ãã
ã¾ããClosure渡ãããããªãäºå®ãªã®ã§ãããã¾ãæ°ã«ãªããªããã ãã©ã
ã¡ãªã¿ã«ãã¯ã©ã¹ã§åãããå ´åã¯ãã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã«Container instanceãããã£ã¦ãããããã
â¦â¦ä»åº¦ã¤ãããããã¡ããskeletonãã«ã¯ãåºåºã¯ã©ã¹ãæ¸ãã¦ããã¾ããããã
Redirect helper
$app = new \Slim\App(); $app->redirect('/books', '/library', 301);
身ããµãããªãï½
ç´è¿ãä»äºã§ã¯ä½¿ããªãæ°ããããã ãã©ããªããã®æã«ä½¿ããããªã®ã§ãã»ãã®ãã¨è¨æ¶ãã¦ããã¾ããã
ãã³ã³ãã³ãã®å¼ã£è¶ãè¾¼ã¿ã®æ¹ä¿®ãã¨ãã§ä½¿ããããªæ°ããããã ã
Route strategies
ãµãâ¦â¦ãURIã«å
¥ã£ã¦ãããã©ã¡ã¿å¼æ°ãã¾ããã®ã話ãã
$c = new \Slim\Container(); $c['foundHandler'] = function() { return new \Slim\Handlers\Strategies\RequestResponseArgs(); }; $app = new \Slim\App($c); $app->get('/hello/{name}', function ($request, $response, $name) { return $response->write($name); });
http://example.com:8080/hello/gallu
gallu
ã§
$c = new \Slim\Container(); $c['foundHandler'] = function() { return new \Slim\Handlers\Strategies\RequestResponseArgs(); }; $app = new \Slim\App($c); $app->get('/hello/{name}/{age}', function ($request, $response, $name, $age) { return $response->write($name . ':' . $age); });
http://example.com:8080/hello/gallu/100
gallu:100
ã¨ããµãã
ãªãã ãããªãã好ã¿ã®åé¡ããªæ°ããããªãå²ã¨ã
â¦â¦å¤åã使ããªãäºæï½
Route placeholdersã
ããæ£ã
使ã£ã¦ãï½
ãã ããã®æ¸å¼ã¯ä»ã¾ã§åºã¦ããªãã£ããããããã ããã§ãã¯ããªã
$app->get('/users[/{id}]', function ($request, $response, $args) { // responds to both `/users` and `/users/123` // but not to `/users/` });
$app->get('/news[/{year}[/{month}]]', function ($request, $response, $args) { // reponds to `/news`, `/news/2016` and `/news/2016/03` });
ãã®è¾ºã¯ã¾ãããã£ã¦ããªãã¦ãããªè¨è¿°ã ããç解ã
ã¡ããã¨è©¦ãããã®ããããã
$app->get('/news[/{params:.*}]', function ($request, $response, $args) { $params = explode('/', $args['params']); // $params is an array of all the optional segments });
ãµãã
å®é¨ã
$app = new \Slim\App; $app->get('/news[/{params:.*}]', function ($request, $response, $args) { $params = explode('/', $args['params']); var_dump($args, $params); // $params is an array of all the optional segments });
array(1) { ["params"]=> string(0) "" } array(1) { [0]=> string(0) "" }
http://example.com:8080/news/hoge
array(1) { ["params"]=> string(4) "hoge" } array(1) { [0]=> string(4) "hoge" }
http://example.com:8080/news/hoge/100
array(1) { ["params"]=> string(8) "hoge/100" } array(2) { [0]=> string(4) "hoge" [1]=> string(3) "100" }
ããé¢ç½ãã
Regular expression matching
ã§ããã±ããããããã®ããâ¦â¦â¦ã£ã¦ãä¸è¿°ã®ã.*ãã£ã¦ãæ£è¦è¡¨ç¾ããç解ã
ãªãã¦ã®ã¯ç½®ãã¨ãã¦ã軽ããµã³ãã«ã³ã¼ãã
$app = new \Slim\App(); $app->get('/users/{id:[0-9]+}', function ($request, $response, $args) { // Find user identified by $args['id'] });
ã©ããããã®æ£è¦è¡¨ç¾ã使ããã®ããããï¼ï¼
ãããããæ£è¦è¡¨ç¾ããã¾ã使ããªããããä¸æ
£ãä¸ã®ä¸æ
£ããã§ã¯ãããã ãã©ãã¾ããã®è¾ºã¤ããã¨ãããããã¨ä¾¿å©ã«ã§ãããã§ãããã¾ãã
ã¾ãä¸æ
£ããªã®ã§ãå¿
è¦ã«ãªã£ã¦ãã調ã¹ãã¨ãã¾ããã*2ã
Route names
ããããããã¯ããªãã¤ã¬ã¯ãããæã«ååã§ãªãã¤ã¬ã¯ããããããç¨éã ããããã以å¤ã£ã¦ããã®ããããï¼ï¼
$app = new \Slim\App; $app->get('/', function(Request $request, Response $response, array $args) { echo "hoge test\n"; })->setName('top'); $app->get('/hoge', function(Request $request, Response $response, array $args) { echo $this->get('router')->pathFor('top'); });
/
ãããäºæ³éãã
ããããªãã¤ã¬ã¯ãã®ã¡ã½ããã§åæã«è§£æ±ºã§ãããããã¨æ¥½ããã ãªããã¨ããèãã¦ã¿ãããããã
å®è£
ããã¨ãã«ãã¡ãã£ã¨ã ããèãã¦ã¿ã¾ããã
Route groups
ããããã°ã«ã¼ãã³ã°ã
$app = new \Slim\App(); $app->group('/users/{id:[0-9]+}', function () { $this->map(['GET', 'DELETE', 'PATCH', 'PUT'], '', function ($request, $response, $args) { // Find, delete, patch or replace user identified by $args['id'] })->setName('user'); $this->get('/reset-password', function ($request, $response, $args) { // Route for /users/{id:[0-9]+}/reset-password // Reset the password for user identified by $args['id'] })->setName('user-password-reset'); });
ãã°ã«ã¼ããã¿ã¼ã³ã®ãã¬ã¼ã¹ãã«ãå¼æ°ã¯ãæçµçã«ãã¹ããããã«ã¼ãã§ä½¿ç¨ã§ããããã«ãªãã¾ãã(æ©æ¢°ç¿»è¨³)ãããããããã¤ã³ãã£ã¡ãããã¤ã³ãããããããããã使ããªããã ã¨ãããããã¨æ´ãã ãã¨æãã(ç¬
ãã¨ã¯ãMiddlewareãããã使ãã®ã«
$app->group('', function() { $this->get('/billing', function ($request, $response, $args) { // Route for /billing }); $this->get('/invoice/{id:[0-9]+}', function ($request, $response, $args) { // Route for /invoice/{id:[0-9]+} }); })->add( new SharedMiddleware() );
ãªãã¦ã®ãããã½ããã
ãã¤ã³ãã¯ãgroupsã®ä¸ã§ã¯ã$appãããªãã¦$thisã使ããã¦ããããããã
ãNote inside the group closure, $this is used instead of $app. Slim binds the closure to the application instance for you, just like it is the case with route callback binds with container instance.ãã ã£ã¦ã
ãã®è¾ºã¯ã¾ããã¡ããã£ã¨æ°ãä»ãã¦ãããã¾ããã
ã¾ããgroupã®ä¸ã§$appã¤ãã£ããæãããããããã«æ°ã¥ããã ãããã©ãã(ç¬
ãã®è¾ºæãããªã
$app->group('', function() use($app) { $app->get('/billing', function ($request, $response, $args) { // Route for /billing }); $app->get('/invoice/{id:[0-9]+}', function ($request, $response, $args) { // Route for /invoice/{id:[0-9]+} }); });
ã£ã¦æ¸ãæ¹ãæ¨å¥¨ãã¦ããã¦ããããã®ãããªããçãªã
ã©ã£ã¡ããªãï¼ æ©ãã
Route middleware
ãYou can also attach middleware to any route or route group. Learn more.*3ãã»ã¸ã
å¥URIãªã®ãã
ããããå¥ç¨¿ã§ã確èªãã¾ããã
Router caching
ãããå¥URIããã ãè¨å®ããã®ã¯ããã¡ã¤ã«åãã ããã£ã½ããã ããã
è¨å®ã¯ãrouterCacheFileãã
ãã®ãã¡è©¦ãã¦ã¿ã¾ããã
Container Resolution
ãµãâ¦â¦
ã»container_key:method
ã»Class:method
ã»An invokable class
ã»container_key
ãããªããã¾ããã¡ã½ããåã®ããã©ã__invokableãã ã£ãã®ã§ãä¸çªç®ã¯ãªãã¨ãªãããããã ãã
ãã¦ãã³ã¼ãä¾ã
$app->get('/', '\HomeController:home'); $app->get('/', \HomeController::class . ':home');
ããããªãã¨ãªããäºçªç®ã®ã»ããããã好ã¿ãããªãã
ã§â¦â¦ãcontainer_keyããããããã¶ããã¯ã©ã¹åãããã®ã¾ã¾keyã¨ãã¦å
¥ãã¦ããã ãããã
ãæ ¹æ lessãªãã ãã©å¾®å¦ã«ä¸å®ã®åããããã£ãããããã®å®è£
ã
ã¾ãå¤åãã»ã¨ãã©ã®å ´åã§ã¯ãç¹ã«åé¡ãªãããã ã¨ãããã®ã§ãæåã ãè¦ãã¦ããã¾ããã
ãããããåããã¾ãã«å°ãã¤ã³ã¹ã¿ã³ã¹ã«å°ç´°å·¥ããããæã«ä½¿ããæ¹æ³ãªãã ãããªããã£ã¦æãã
$container = $app->getContainer(); $container['HomeController'] = function($c) { $view = $c->get("view"); // retrieve the 'view' from the container return new HomeController($view); };
ããã§
$app->get('/', 'HomeController:home');
ã£ã¦å¼ã¹ã°ããããã¯ãã
ã¾ããã®è¾ºããå®éã«å¿
è¦ã«ãªã£ããããæ¤è¨¼ãã¦ã¿ã¾ããã
Allow Slim to instantiate the controller
ãAlternatively, if the class does not have an entry in the container, then Slim will pass the containerâs instance to the constructor. You can construct controllers with many actions instead of an invokable class which only handles one action.ãããã³ã¼ãèªãã ã
ã ãã
class BaseController { protected $container; // constructor receives container instance public function __construct(ContainerInterface $container) { $this->container = $container; } }
ã£ã¦ã®ãä½ãã¨æ±ãããããã ãããªããã£ã¦æã£ã¦ãã
Using an invokable class
ã«ã¤ãã¦ã¯ãã©ããã£ããªããç¨åº¦ã
$app->get('/', \HomeAction::class);
ã§ãããã®ã¯ã¡ãªãããããããã«æãããã®ã§ããï¼Controllerã¯ã©ã¹ãããï¼Actionãã§ãããªããããããããªã®ãããªããã¨ã¯æãããªã©ã
æã£ããã常èçãªé·ãã§è§£æãçµããã¾ããâ¦â¦â¦Middlewareãå¥URIã ã£ããã(ç¬
*1:å¾è¿°ãã¾ãããæ£è¦è¡¨ç¾ä½¿ãã®ããå¤åãä¸çªç¢ºå®
*2:ãªãã¦ãã¨ãã£ã¦ããããã¤ã¾ã§ãä¸æ £ããªã®ã ã
*3:https://www.slimframework.com/docs/v3/concepts/middleware.html