Slimã¨Slim-Skeleton ã®index.php
å ¬å¼ãµã¤ã http://www.slimframework.com/ ã®ãµã³ãã«ã ã¨ãindex.phpã¯
<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; require 'vendor/autoload.php'; $app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response, array $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run();
ã¨æ¸ããã¦ããä¸æ¹ã§ããå®éã«ããã¸ã§ã¯ããèµ·ããæã«ãã¼ã¹ã«ä½¿ãã¨ä¾¿å©ãã¨ããã¦ãããå
¬å¼ããåºã¦ããSlim-Skeletonã®index.php ã§ã¯ãå°ãã°ãããå·®ç°ãããã¾ãã
ãã®è¾ºãå°ããèªã¿è§£ãã¦ããã¾ãããã
注æ
ä¸ææ以éãçµæ§ãã£ããã¨æ¸ãæ¹ãå¤ãã£ã¦ãã¾ãã
ä¸ç«¯ https://github.com/slimphp/Slim-Skeleton/tree/439b6ec6cb1189d0be7fe31f87af4cbeeb801e53 ãåæã«è¨è¿°ãå¾ã§ææ°ç¨ã®ãã©ãã¼ã¢ããããã¾ãã
ãã¦ã
ã¨ããããã¾ãã¯ã«ã¼ãã£ã³ã°ã®è¨å®ã§ãã
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; });
ãã®ããããã§ããã
ããããPageãå¢ããæ¯ã«index.phpã«éãã¦ããã¨ãindex.phpãããããã¨é·ããªã£ã¦ãã¾ãã®ã§ãã¡ããã¨åãåºãã¦ã¿ã¾ãããã
ããã§ãããpublicã«ããã®ãäºã好ã¾ããã«æ¬ ãããã¨æãã¾ãã®ã§ã
publicãã£ã¬ã¯ããªã¨åãã¬ãã«ã«ãsrcã¨ãããã£ã¬ã¯ããªãåã£ã¦ããã®ä¸ã« routes.php ã¨ããããã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãããã
ããããã¨ãã¾ãåãåºãã®ã§ãindex.phpã
<?php require 'vendor/autoload.php'; $app = new \Slim\App; // Register routes require __DIR__ . '/../src/routes.php'; $app->run();
ãããªããåãåºããroutes.phpã
<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; // Routes $app->get('/[{name}]', function (Request $request, Response $response, array $args) { // Sample log message $this->logger->info("Slim-Skeleton '/' route"); // Render index view return $this->renderer->render($response, 'index.phtml', $args); });
ãããªãã¾ãã
useå¥ã¯ãåãåºããã»ãã®ã³ã¼ãã§ä½¿ã£ã¦ãã®ã§ãä½µãã¦ã®ç§»åã§ããã
ã¤ãã§ã«ã Psr\Http\Message\ServerRequestInterface ã¯ã¤ã³ã¿ãã§ã¼ã¹å®£è¨ã ããªã®ã§ï¼Slimã¯å®éã«ã¯ã¡ããã¨ã¯ã©ã¹ã¾ã§åã£ã¦ããã®ã§ã
implementsããã¯ã©ã¹ã«ãuseå¥ãå¤æ´ãã¦ããã¾ãããã
<?php use Slim\Http\Request; use Slim\Http\Response; // Routes $app->get('/[{name}]', function (Request $request, Response $response, array $args) { // Sample log message $this->logger->info("Slim-Skeleton '/' route"); // Render index view return $this->renderer->render($response, 'index.phtml', $args); });
ãã¦ã
index.phpã§ããã
ãã®ã¾ã¾ã§ãããã®ã§ãããå®éã«ã¢ããªã±ã¼ã·ã§ã³ãçµãã®ã§ããã°ããªã«ãããã®è¨å®å¤ãã¯ããã¯ã欲ããã¨ããã«ãªãã¾ãã
new \Slim\App ã§ããã³ã³ã¹ãã©ã¯ã¿ã«è²ã
ã¨å®éã«ã¯è¨å®ã渡ãäºãã§ããã®ã§ã
ã¨è¨ãããã§ãè¨å®å¤ããã¯ãåãåºãã¦è¨è¼ãã¦ã¿ã¾ãããã
ãã¡ã¤ã«ã¯ãsrc/settings.php ã«ãã¦ã¿ã¾ãã
index.php
<?php require 'vendor/autoload.php'; // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Register routes require __DIR__ . '/../src/routes.php'; $app->run();
settings.php
<?php return [ 'settings' => [ 'displayErrorDetails' => true, // set to false in production 'addContentLengthHeader' => false, // Allow the web server to send the content-length header // Renderer settings 'renderer' => [ 'template_path' => __DIR__ . '/../templates/', ], // Monolog settings 'logger' => [ 'name' => 'slim-app', 'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log', 'level' => \Monolog\Logger::DEBUG, ], ], ];
ããã§ãsettings.phpã«è¨å®ãæ¸ããã¨ãnew \Slim\Appã®å¼æ°ã¨ãã¦æ¸¡ããããã®ã§ãè²ã ãªè¨å®ãå ¥ãè¾¼ãäºãã§ããããã«ãªãã¾ãã
次ã«ã
diã³ã³ãããå°ãè¨å®ããã¦ããã¾ãããã
ã¨ãããããã£ããã¨ãviewç¨ã®ã¯ã©ã¹ã¨ãã¦PhpRendererãã¨ãæä½éã®ãlogger(ãã°ãåºåããã¯ã©ã¹)ããããã¯è¿½å ãã¦ããã¾ãããã
追å ç¨ã®è¨å®ã¯ src/dependencies.php ã«ã§ãããã¦ãããã¨ã«ãã¾ãã
index.php
<?php require 'vendor/autoload.php'; // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Set up dependencies require __DIR__ . '/../src/dependencies.php'; // Register routes require __DIR__ . '/../src/routes.php'; $app->run();
dependencies.php
<?php // DIC configuration $container = $app->getContainer(); // view renderer $container['renderer'] = function ($c) { $settings = $c->get('settings')['renderer']; return new Slim\Views\PhpRenderer($settings['template_path']); }; // monolog $container['logger'] = function ($c) { $settings = $c->get('settings')['logger']; $logger = new Monolog\Logger($settings['name']); $logger->pushProcessor(new Monolog\Processor\UidProcessor()); $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level'])); return $logger; };
ããã§ä¸æ®µè½â¦â¦ãããã¨ãããªã®ã§ããã
ã¤ãã§ã«ãmiddlewareç¨ã®è¨å®ããããå ´æãä½ã£ã¦ããã¾ãããã
ä¸ç«¯ãå ´æã ãããä½ãæãã§ã src/middleware.php ã«ç¨æããã¦ããã¾ãã
index.php
<?php require 'vendor/autoload.php'; // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Set up dependencies require __DIR__ . '/../src/dependencies.php'; // Register middleware require __DIR__ . '/../src/middleware.php'; // Register routes require __DIR__ . '/../src/routes.php'; $app->run();
middleware.php
<?php // Application middleware // e.g: $app->add(new \Slim\Csrf\Guard);
ãããªã¨ããã§ããããã
ã¡ãªã¿ã«ããµã³ãã«ã§æ¸ããã¦ããã\Slim\Csrf\Guardãã¯ããããã®ãã®ã§ã¯ãªãã¦ãããã®ã§ããªã«ãããCSRF対çç¨ã®ãã®ãããã¦ããã®ã¯ã大å¤ã«å¥½ã¾ããã¨æããã¾ãã
ããã§ããããããSlim-Skeletonã®index.phpãã¨åããã®ãã«ãªãã¾ãã
å®éã®Slim-Skeletonã®index.phpã¯
<?php if (PHP_SAPI == 'cli-server') { // To help the built-in PHP dev server, check if the request was actually for // something which should probably be served as a static file $url = parse_url($_SERVER['REQUEST_URI']); $file = __DIR__ . $url['path']; if (is_file($file)) { return false; } } require __DIR__ . '/../vendor/autoload.php'; session_start(); // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Set up dependencies require __DIR__ . '/../src/dependencies.php'; // Register middleware require __DIR__ . '/../src/middleware.php'; // Register routes require __DIR__ . '/../src/routes.php'; // Run app $app->run();
ã¨ãªã£ã¦ãã¦ã
空æ¹è¡ã®ç®æãã®ããã¨
ã»å
é ã®9è¡
ã»session_start();
ãããã®å·®ç°ãã§ããã
å
é ã®9è¡ã¯å¤§éæã«
ã»ãã«ãã¤ã³ã¦ã§ããµã¼ãã¼ã使ã£ã¦ããæã«ãstaticãªURI(å®å¨ãããã¡ã¤ã«ã®URI)ãcallãããããããã°ã©ã çãªå¦çãããªããããã®ãã¸ãã¯
ã«ãªãã¾ãã
session_start()ã¯ããã®ã¾ã¾ã
ã¾ãã»ãã·ã§ã³ã使ããã¨ã¯ã極ãã¦å¤ããã§ãããããã
ã¨ããã®ãããªæãã§ãplainã®Slimã®index.phpãããSlim-Skeletonã®index.phpã«ç§»ãå¤ãããè¡ããã¦ãã¾ãã
â¦â¦ã¨ããæãã ã£ãã®ã§ããã6 Nov 2018ã®
https://github.com/slimphp/Slim-Skeleton/tree/8dd2f8469514a43d4180148466a832f8d1683fe4
ã®ã¿ã¤ãã³ã°ã§ãã¡ããã¨è«¸ã
ãããã£ã¨æ§å¤ããããããããªã®ã§ãå°ãè¦ãç´ãã¦ã¿ã¾ãããã
index.phpã§ããããããªé¢¨ã«ãªã£ã¦ãã¾ãã
<?php if (PHP_SAPI == 'cli-server') { // To help the built-in PHP dev server, check if the request was actually for // something which should probably be served as a static file $url = parse_url($_SERVER['REQUEST_URI']); $file = __DIR__ . $url['path']; if (is_file($file)) { return false; } } require __DIR__ . '/../vendor/autoload.php'; session_start(); // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Set up dependencies App\Dependencies::init($app); // Register middleware App\Middleware::init($app); // Register routes App\Routes::init($app); // Run app $app->run();
å
¨ä½çã«ãã¯ã©ã¹å::init($app)ãã®æ¹æ³ã§ã®callã«å¤ãã£ã¦ããæãã§ããã
ä¾ãã°Routesãè¦ãã¨
<?php namespace App; use Slim\App; use Slim\Http\Request; use Slim\Http\Response; /** * Configures the routes * @param App $app */ class Routes { /** * Configures the routes * @param App $app */ public static function init(App $app) { $container = $app->getContainer(); $app->get('/[{name}]', function (Request $request, Response $response, array $args) use ($container) { // Sample log message $container->get('logger')->info("Slim-Skeleton '/' route"); // Render index view return $container->get('renderer')->render($response, 'index.phtml', $args); }); } }
ã¨ããæãã§ãå ¨ä½çã«ãã¯ã©ã¹ã®ä¸ã®staticãªã¡ã½ããã§ã®å®ç¾©ãã«å¤ãã£ã¦ããããã§ãã
ã§ããã®ç¶æ³ãããããäºé±éå¼±ã19 Nov 2018 ã«
https://github.com/slimphp/Slim-Skeleton/tree/15fc5968d9430e67110d82dff3b1b3a2349c9cf6
ã§ãããä¸åº¦å¤ãã£ã¦ãã¾ãã
<?php if (PHP_SAPI == 'cli-server') { // To help the built-in PHP dev server, check if the request was actually for // something which should probably be served as a static file $url = parse_url($_SERVER['REQUEST_URI']); $file = __DIR__ . $url['path']; if (is_file($file)) { return false; } } require __DIR__ . '/../vendor/autoload.php'; session_start(); // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); // Set up dependencies $dependencies = require __DIR__ . '/../src/dependencies.php'; $dependencies($app); // Register middleware $middleware = require __DIR__ . '/../src/middleware.php'; $middleware($app); // Register routes $routes = require __DIR__ . '/../src/routes.php'; $routes($app); // Run app $app->run();
ã§ãroutesã
<?php use Slim\App; use Slim\Http\Request; use Slim\Http\Response; return function (App $app) { $container = $app->getContainer(); $app->get('/[{name}]', function (Request $request, Response $response, array $args) use ($container) { // Sample log message $container->get('logger')->info("Slim-Skeleton '/' route"); // Render index view return $container->get('renderer')->render($response, 'index.phtml', $args); }); };
ååãstaticãªã¯ã©ã¹ã¡ã½ãããã ã£ãã®ããä»åã¯ãç¡åé¢æ°ãã«ç½®ãæãã£ã¦ããæãã§ããã
ãªããè²ã ã¨åãããããããªæããªã®ã§ãã¡ãã£ã¨èå³æ·±ãæãã¾ããã