zend-expressiveã«è¦ãPSR-7æ代ã®PHPãã¬ã¼ã ã¯ã¼ã¯
Zendã«ããæ°ãããã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ãzend-expressiveã®ãã¼ã¸ã§ã³1.0.0rc2ããªãªã¼ã¹ããã¾ããã
ä»æ¥ã¯ããã®PSR-7対å¿ã®zend-expressiveãã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãã
zend-expressiveã®ã¤ã³ã¹ãã¼ã«
æãç°¡åãªã¤ã³ã¹ãã¼ã«æ¹æ³ã§ãããã¹ã±ã«ãã³ããã¸ã§ã¯ããComposerã§ã¤ã³ã¹ãã¼ã«ããæ¹æ³ã§ã¤ã³ã¹ãã¼ã«ãã¾ãã
$ composer create-project -s rc zendframework/zend-expressive-skeleton ze
ã«ã¼ã¿ã®é¸æ
ã¾ãã以ä¸ã®ããã«ã«ã¼ã¿ãã©ãã«ãããèããã¾ãã
Installing zendframework/zend-expressive-skeleton (1.0.0rc2)
- Installing zendframework/zend-expressive-skeleton (1.0.0rc2)
Loading from cache
Created project in ze
> App\Composer\OptionalPackages::install
Setup data and cache dir
Setting up optional packages
Which router you want to use?
[1] Aura.Router
[2] FastRoute
[3] Zend Router
Make your selection or type a composer package name and version (FastRoute):
ããã©ã«ãã¯FastRouteã§ãããAura.RouterãZend Routerãé¸ã¹ã¾ãã
ã¨ãããããããã©ã«ãã®ã¾ã¾ [return] ãã¼ãæ¼ãã¦é²ã¿ã¾ãã
Make your selection or type a composer package name and version (FastRoute):
- Adding package zendframework/zend-expressive-fastroute (^0.2)
- Copying /config/autoload/routes.global.php
zendframework/zend-expressive-fastrouteã追å ããã¾ããã
DIã³ã³ããã®é¸æ
ç¶ãã¦DIã³ã³ãããã©ãã«ãããèããã¾ãã
Which container you want to use for dependency injection?
[1] Aura.Di
[2] Pimple-interop
[3] Zend ServiceManager
Make your selection or type a composer package name and version (Zend ServiceManager):
ããã©ã«ãã¯Zend ServiceManagerã§ãããAura.Diã¨Pimple-interopãé¸ã¹ã¾ãã
ããã©ã«ãã®ã¾ã¾ [return] ãã¼ãæ¼ãã¦é²ã¿ã¾ãã
Make your selection or type a composer package name and version (Zend ServiceManager):
- Adding package zendframework/zend-servicemanager (^2.5)
- Adding package ocramius/proxy-manager (^1.0)
- Copying /config/container.php
zendframework/zend-servicemanagerãªã©ã追å ããã¾ããã
ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®é¸æ
次ã¯ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®é¸æã§ãã
Which template engine you want to use?
[1] Plates
[2] Twig
[3] Zend View installs Zend ServiceManager
[n] None of the above
Make your selection or type a composer package name and version (n):
ããã©ã«ãã¯ãªãã§ãããPlatesãTwigãZend Viewãé¸æã§ãã¾ãã
ä»åã¯ããã©ã«ãã®ãªãã«ãã¾ãã
ã¨ã©ã¼ãã³ãã©ã®é¸æ
éçºæã«ä½¿ç¨ããã¨ã©ã¼ãã³ãã©ã®é¸æã§ãã
Which error handler do you want to use during development?
[1] Whoops
[n] None of the above
Make your selection or type a composer package name and version (Whoops):
Whoopsããé¸æè¢ã¯ãªãã®ã§ãWhoopsã«ãã¦ããã¾ãã
Make your selection or type a composer package name and version (Whoops):
- Adding package filp/whoops (^1.1)
- Copying /config/autoload/errorhandler.local.php
filp/whoopsã追å ããã¾ããã
ããã¦ãããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ãéå§ããã¾ãã
ã¤ã³ã¹ãã¼ã«ãããããã±ã¼ã¸ã¯ä»¥ä¸ã§ããã
$ composer show -i
composer/composer 1.0.0-alpha10
container-interop/container-interop 1.1.0
doctrine/instantiator 1.0.5
filp/whoops 1.1.7
justinrainbow/json-schema 1.5.0
nikic/fast-route v0.6.0
ocramius/proxy-manager 1.0.2
phpdocumentor/reflection-docblock 2.0.4
phpspec/prophecy v1.5.0
phpunit/php-code-coverage 2.2.4
phpunit/php-file-iterator 1.4.1
phpunit/php-text-template 1.2.1
phpunit/php-timer 1.0.7
phpunit/php-token-stream 1.4.8
phpunit/phpunit 4.8.15
phpunit/phpunit-mock-objects 2.3.8
psr/http-message 1.0
roave/security-advisories dev-master 43ac5a3
sebastian/comparator 1.2.0
sebastian/diff 1.3.0
sebastian/environment 1.3.2
sebastian/exporter 1.2.1
sebastian/global-state 1.1.1
sebastian/recursion-context 1.0.1
sebastian/version 1.0.6
seld/jsonlint 1.3.1
squizlabs/php_codesniffer 2.3.4
symfony/console v2.7.5
symfony/finder v2.7.5
symfony/process v2.7.5
symfony/yaml v2.7.5
zendframework/zend-code 2.5.2
zendframework/zend-diactoros 1.1.4
zendframework/zend-escaper 2.5.1
zendframework/zend-eventmanager 2.5.2
zendframework/zend-expressive 1.0.0rc2
zendframework/zend-expressive-fastroute 0.2.0
zendframework/zend-hydrator 1.0.0
zendframework/zend-servicemanager 2.6.0
zendframework/zend-stdlib 2.7.4
zendframework/zend-stratigility 1.1.2
ãã®ããã«zend-expressiveã§ã¯ãã«ã¼ã¿ãDIã³ã³ããããã³ãã¬ã¼ãã¨ã³ã¸ã³ãéçºæã®ã¨ã©ã¼ãã³ãã©ãæåããé¸æå¯è½ã«ãªã£ã¦ãã¾ãã
ãã®ãã¬ã¼ã ã¯ã¼ã¯ã使ã£ã¦ããããã«ã¼ã¿ã¯ãã¬ã¼ã ã¯ã¼ã¯ã«ä»å±ãã¦ãããã®ãã使ããªããªã©ã¨ãããã«ãã«ãããã¨ã¯ããã¾ããã
Webãµã¼ãã®èµ·å
PHPãã«ãã¤ã³Webãµã¼ããèµ·åãã¦ã¿ã¾ãããã
$ php -S 127.0.0.1:8080 -t public/
http://localhost:8080/
ã«ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ããã¨ã以ä¸ã®JSONãè¦ããã¨ãã§ãã¾ãã
{"welcome":"Congratulations! You have installed the zend-expressive skeleton application.","docsUrl":"zend-expressive.readthedocs.org"}
ãã¦ããã®å¤ã¯ã©ãããæ¥ã¦ããã®ã§ãããï¼
ã«ã¼ãã£ã³ã°
ã«ã¼ãã£ã³ã°ã®è¨å®ã¯ã以ä¸ã«ããã¾ããã
config/autoload/routes.global.php
<?php
return [
'dependencies' => [
'invokables' => [
Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
],
],
'routes' => [
[
'name' => 'home',
'path' => '/',
'middleware' => App\Action\HomePageAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'api.ping',
'path' => '/api/ping',
'middleware' => App\Action\PingAction::class,
'allowed_methods' => ['GET'],
],
],
];
ãã¼routes
ã®æåã®é
å
[
'name' => 'home',
'path' => '/',
'middleware' => App\Action\HomePageAction::class,
'allowed_methods' => ['GET'],
],
ãããã»ã©ã®JSONåºåã®ã«ã¼ãã§ããã¯ã©ã¹åãApp\Action\HomePageAction
ã§ãããã¨ããããã¾ãã
ã¢ã¯ã·ã§ã³
App\Action\HomePageAction
ã¯ã©ã¹ã¯ä»¥ä¸ã«ããã¾ãã
src/Action/HomePageAction.php
<?php
namespace App\Action;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router;
use Zend\Expressive\Template;
class HomePageAction
{
private $router;
private $template;
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
{
$this->router = $router;
$this->template = $template;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$data = [];
if ($this->router instanceof Router\AuraRouter) {
$data['routerName'] = 'Aura.Router';
$data['routerDocs'] = 'http://auraphp.com/packages/Aura.Router/';
} elseif ($this->router instanceof Router\FastRouteRouter) {
$data['routerName'] = 'FastRoute';
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
} elseif ($this->router instanceof Router\ZendRouter) {
$data['routerName'] = 'Zend Router';
$data['routerDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html';
}
if ($this->template instanceof Template\PlatesRenderer) {
$data['templateName'] = 'Plates';
$data['templateDocs'] = 'http://platesphp.com/';
} elseif ($this->template instanceof Template\TwigRenderer) {
$data['templateName'] = 'Twig';
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
} elseif ($this->template instanceof Template\ZendViewRenderer) {
$data['templateName'] = 'Zend View';
$data['templateDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html';
}
if (!$this->template) {
return new JsonResponse([
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
'docsUrl' => 'zend-expressive.readthedocs.org',
]);
}
return new HtmlResponse($this->template->render('app::home-page', $data));
}
}
__invoke()
ã¡ã½ããå
ã«ãªãããã¡ããã¡ãifæãããã¾ãããä»ã¯ãã³ãã¬ã¼ãã¨ã³ã¸ã³ããªãã®ã§ãçµå±ä»¥ä¸ã®ifæã ããå®è¡ããã¦ãããã¨ããããã¾ãã
if (!$this->template) {
return new JsonResponse([
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
'docsUrl' => 'zend-expressive.readthedocs.org',
]);
}
ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãã¤ã³ã¹ãã¼ã«ããæ¹ããã¼ã¸ã®è¦æ ããããã£ãã¿ããã§ããã
ã¾ã¨ã
PHPã¯PSR-7æ代ã«çªå ¥ãã¾ããããã¬ã¼ã ã¯ã¼ã¯ã®æ½è±¡åãã³ã³ãã¼ãã³ãåã¯ããã«é²ã¿ã¾ãããã§ã«ã«ã¼ã¿ãDIã³ã³ããããããã®ããã«çµã¿æ¿ãå¯è½ã§ãã
åè
Date: 2015/10/23