ç¡åã¯ã©ã¹(PHP7)ããã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ã§ä½¿ã£ã¦ã¿ã
PHP7ããã¯ç¡åã¯ã©ã¹ã使ããããã«ãªãã¾ããã
ã©ããã£ãå ´åã«ç¡åé¢æ°ã使ãæ©ä¼ãããããªãã¨èãã¦ããã¨ããµã¨ãã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ã®ç¡åé¢æ°é¨åã®ä»£ããã«ä½¿ãã¨è¯ãããããªããã¨æããã£ã¦ã¿ã¾ããã¨ããã®ããã®è¨äºã
[追è¨]
- å®éã«Slim3 ã®ã¢ããªã§ä½¿ããããã«composerã§ã¤ã³ã¹ãã¼ã«ã§ããããã«ã³ã¼ããæ´çããªããã¦ç¨æãã¦ã¿ãã
- Slim3 ã§ã¯ãªãããã£ã¨èãPHP7ãã¬ã¼ã ã¯ã¼ã¯ãæ¸ãå§ãã¦ã¿ãè¨äºã¯ãã£ã¡ã
ç¡åé¢æ° vs ç¡åã¯ã©ã¹
ã¾ããPSR-7ã§ãªã¯ã¨ã¹ãã¨ã¬ã¹ãã³ã¹ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã決ããããããã«å¾ã£ããã¬ã¼ã ã¯ã¼ã¯ãããã«ã¦ã§ã¢ãåºã¦ãã¦ãã¾ããããã§ãPSR-7 ã®ãµãã¼ãããã Slim3 㨠PHP7 試ãã¦ã¿ã¾ãã
ã¾ãã¯ãSlim3 ã®ããã¥ã¡ã³ãéãã« Hello World ããã¨:
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require __DIR__ . '/../vendor/autoload.php'; $c = new \Slim\Container; $c['greet'] = 'Hello '; $app = new \Slim\App($c); $app->get('/hello/{name}', function (Request $request, Response $response, $args) use ($app){ $response->write($app->getContainer()['greet'] . $args['name']); return $response; }); $app->run();
ãªã¯ã¨ã¹ãã¨ã¬ã¹ãã³ã¹ã®ãªãã¸ã§ã¯ããç¡åé¢æ°ã«æ¸¡ããã¦ããã®ã§ãã¬ã³ããªã³ã°ããã¨ã㯠Slimã«ç¨æããã¦ããã¬ã¹ãã³ã¹ã® `write` ã¡ã½ãããå¼ã³ã¾ãã
ã·ã³ãã«ã§è¯ãã®ã§ãããæ¯å Request 㨠Response ãæèãã¦ã³ã¼ãæ¸ããªãã¦ãæ¸ããããã®æ½è±¡åã欲ãããªãã¾ããããã§ãMyControllerã¯ã©ã¹ãç¨æãããã㧠Requestã¨Responseã¸ã®å¦çãè¡ãããã«ãã¦ã¿ãã®ã以ä¸ã®ã³ã¼ã:
<?php ... $app->get('/hello/{name}', new class($app) extends MyController { public function action($args) { return $this->render($this->container['greet'] . $args['name']); } });
MyControllerãæ½è±¡ã¯ã©ã¹ã¨ãã¦å®ç¾©ããããªããactionã¡ã½ãããå¼ã°ããããã«ãã¦ããã¾ãã
ãªã®ã§ããã®ç¡åã¯ã©ã¹ã§ã¯ actionã¡ã½ãããæ¸ããã¨ã§åã«ã¼ãã£ã³ã°ãã¨ã«å¦çãè¡ãããã¨ããä»çµã¿ã§ãã
ã¾ããã¬ã¹ãã³ã¹ãªãã¸ã§ã¯ãã¯ç´æ¥ããããrenderã¡ã½ãããç¨æãã¦åä½ããããã«ãã¦ã¿ã¾ããã
ããã§ãã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ã§ç¡åé¢æ°ã§ã¯å¤§å¤ã ã£ãæ½è±¡åãç°¡åã«è¡ãã¾ãããããæå³ãªã¬ãªã¬ãã¬ã¼ã ã¯ã¼ã¯ãç°¡åã«ã§ãã¾ãã
ãã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ã ãã§ã¯ã«ã¼ãã£ã³ã°æ°ãå¢ãã¦ããã¨ç¡åé¢æ°ã ãã§ã¯å®è£ ãé£ãããªã£ã¦ããé¨åããæ¯åã¯ã©ã¹ãå®ç¾©ã`__invoke`ãå©ç¨ããæ¹å¼ã§ã¯ãªããããç¨åº¦ç¡åã¯ã©ã¹ã§ãããæ¸ããã¨ããæ¹å¼ã¯ã¡ãªãããããããã§ãã
Twigãã³ãã¬ã¼ãã使ãããã«Traitã§
Slim3 㯠Pimpleãã¼ã¹ã®ãµã¼ãã¹ã³ã³ãããããã®ã§ããã³ãã¬ã¼ãã¨ã³ã¸ã³(twig)ã `view` ã«ä»¥ä¸ã®ããã«ã»ãããããã¨ã§
<?php $app = new \Slim\App(); // Get container $container = $app->getContainer(); // Register component on container $container['view'] = function ($container) { $view = new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ]); $view->addExtension(new \Slim\Views\TwigExtension( $container['router'], $container['request']->getUri() )); return $view; };
ã«ã¼ãã£ã³ã°ã§ä»¥ä¸ã®ããã«viewã使ã£ã¦ã¬ã³ããªã³ã°ãã§ãã¾ãã
<?php $app->get('/hello/{name}', function ($request, $response, $args) { return $this->view->render($response, 'profile.html', [ 'name' => $args['name'] ]); });
ãããããã¬ã¤ãã使ã£ã¦éã£ãã¢ããã¼ãã§ä½¿ããããã«ãã¦ã¿ã¾ããã
ãã¬ã¤ããç¨æãã¦ãããã§Twigã®è¨å®é¢é£ãæ¸ãã¦ãã¾ãã¾ãã
ãµã¼ãã¹ã³ã³ããã¯å©ç¨ããç´æ¥ viewããããã£ãç¨æãã¦ãã¾ãã
- TwigTemplatable.php
<?php namespace Karen\Controller; use \Psr\Http\Message\ResponseInterface as Response; trait TwigTemplatable { private $templatePath; private $cachePath; private $view; public function useTwig() { $this->templatePath = __DIR__ . '/../../templates/'; $this->cachePath = '/tmp/'; // Register component on container $this->view = new \Slim\Views\Twig($this->templatePath, [ 'cache' => $this->cachePath ]); $this->view->addExtension(new \Slim\Views\TwigExtension( $this->container['router'], $this->container['request']->getUri() )); } public function renderTwig($path, $args) { $this->useTwig(); $this->view->render($this->response, $path, $args); } }
ãããã«ã¼ãã£ã³ã°ã®MyControllerã§useããrenderTwigãéãã¦ã¬ã³ããªã³ã°ãã¾ãã
<?php $app->get('/hello/{name}', new class($app) extends MyController { use TwigTemplatable; public function action($args) { return $this->renderTwig('web.html', ['name' => $args['name']]); } });
ã¢ããªã±ã¼ã·ã§ã³ã大ãããªãã¨ã³ã³ããèªèº«ãè¨ãã¦ããåé¡ãç¡åã¯ã©ã¹ + ãã¬ã¤ããå©ç¨ãããã¨ã§ããç¨åº¦ãã¾ãæ´çã§ããããªæãããã¾ããããããã¾ã§ãããªãã¦ããªãã¨ãæã£ããã
試ãã¦ã¿ã
PHP7ãã¤ã³ã¹ãã¼ã«ããã¦ããã°githubããã³ã¼ãæã£ã¦ããã°è²ã éã¹ã¾ãã
$ git clone [email protected]:brtriver/slim3-anonymous-class.git $ make setup $ make install $ php -S localhost:8888 -t ./web // ãã¨ã¯ãã©ã¦ã¶ãã http://localhost:8888/hello/brtriver ã«ã¢ã¯ã»ã¹ããã°OK