Slim docsã®è§£æ; The Response
https://www.slimframework.com/docs/v3/objects/response.html
ãããçµæ§ãªå¤§ç©ã ãããã¨äºæ³ã
How to get the Response object
ããã¾ãHow toè¨ããã¦ããå¼ã°ããé¢æ°(ã¡ã½ãã)ã®ç¬¬äºå¼æ°ã§ã²ãããããããã ããªãã
<?php use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; $app = new \Slim\App; $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { // Use the PSR 7 $response object return $response; }); $app->run();
ããã¯ä¸å¿çªã£è¾¼ã¿ãç¡ãããªãã¦ã
ãæååãreturnããã¨ãç¾ç¶ã®$responseã«ãã®æååãbodyã¨ãã¦æ¸ãè¾¼ããããªå¦çã追è¨ãã¦ãããããã ããã
http://d.hatena.ne.jp/gallu/20180613/p1
ã®
} elseif (is_string($newResponse)) { // if route callback returns a string, then append it to the response if ($response->getBody()->isWritable()) { $response->getBody()->write($newResponse); } }
ããããåç
§ããã®è¾ºããããããç°¡åã«å¤ãããã£ã¦ã®ãèãã«ãããããæååreturnããããããªã«ãã¦ããããã¯ãæå¾
ãã¦ããåä½ããªããããªãããªãï¼ ã¨æã£ã¦ãã
The PSR 7 response object is injected into your Slim application middleware as the second argument of the middleware callable like this:
<?php use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; $app = new \Slim\App; $app->add(function (ServerRequestInterface $request, ResponseInterface $response, callable $next) { // Use the PSR 7 $response object return $next($request, $response); }); // Define app routes... $app->run();
middlewareå¨ãã¯ã¾ãä»åº¦ãã¾ã¨ãã¦å¾¹åºçã«ããªã®ã§ãä»åã¯ä¸æ¦ããã¹ã
The Response Statusãå²ã¨å¤§äºã½ã
$newResponse = $response->withStatus(302);
ããã ãææ¡ãã¦ããã°ããããããªãããªãâ¦â¦â¦ãããä¸ç¹æ°ã«ãªããã¨ã
Response(以å¤ãããã¤ãããã ãã©)ã¯ããããããä¸å¤ã¤ã³ã¹ã¿ã³ã¹ã§ãããã¨ããã¦ãããããwithStatusã®å®è£
ã£ã¦
vendor/slim/slim/Slim/Http/Response.php
public function withStatus($code, $reasonPhrase = '') { $code = $this->filterStatus($code); if (!is_string($reasonPhrase) && !method_exists($reasonPhrase, '__toString')) { throw new InvalidArgumentException('ReasonPhrase must be a string'); } $clone = clone $this; $clone->status = $code; if ($reasonPhrase === '' && isset(static::$messages[$code])) { $reasonPhrase = static::$messages[$code]; } if ($reasonPhrase === '') { throw new InvalidArgumentException('ReasonPhrase must be supplied for this code'); } $clone->reasonPhrase = $reasonPhrase; return $clone; }
ãªãã ããããã¤ã³ãã¯ã$clone = clone $this;ããã¦ãreturn $clone;ãã£ã¦ãããã
ãªã®ã§ããã®è¾ºã§ãã£ãã®ãæååãreturnãã¨çµã¿åãããã¨ããªããä¸ä¸è´ãåºãããªæ°ãå°ããããã®ã§ãæ°ãä»ããã»ããããããã
â¦â¦â¦ãããreturnã¯ãã¡ããã¨Responseã¤ã³ã¹ã¿ã³ã¹è¿ããããã«ããã»ããå®å
¨ããã
ã¡ã¨ã確èªãã¦ã¿ã¾ããã
public/index.php
$app = new \Slim\App; $app->get('/add_header', function(Request $request, Response $response, array $args) { $response = $response->withStatus(304); //return 'test'; return $response->getBody()->write('test'); });
$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /add_header HTTP/1.1
Host: example.comHTTP/1.1 200 OK
Host: example.com
Date: Mon, 18 Jun 2018 05:07:09 +0000
Connection: close
X-Powered-By: PHP/7.2.6
Content-Type: text/html; charset=UTF-8
Content-Length: 4testConnection closed by foreign host.
â¦â¦â¦ããï¼
â¦â¦â¦â¦ãã
vendor/slim/slim/Slim/Http/Stream.php
public function write($string) { if (!$this->isWritable() || ($written = fwrite($this->stream, $string)) === false) { throw new RuntimeException('Could not write to stream'); } // reset size so that it will be recalculated on next call to getSize() $this->size = null; return $written; }
æ»ãå¤ãstringãã
public/index.php
$app->get('/add_header', function(Request $request, Response $response, array $args) { $response = $response->withStatus(304); $response->getBody()->write('test'); //return 'test'; return $response; });
$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /add_header HTTP/1.1
Host: example.comHTTP/1.1 304 Not Modified
Host: example.com
Date: Mon, 18 Jun 2018 05:18:11 +0000
Connection: close
X-Powered-By: PHP/7.2.6Connection closed by foreign host.
ããæå³éãã
ã£ã¦ãã¨ã¯
$app->get('/add_header', function(Request $request, Response $response, array $args) { $response = $response->withStatus(304); //$response->getBody()->write('test'); return 'test'; });
$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /add_header HTTP/1.1
Host: example.comHTTP/1.1 200 OK
Host: example.com
Date: Mon, 18 Jun 2018 05:19:06 +0000
Connection: close
X-Powered-By: PHP/7.2.6
Content-Type: text/html; charset=UTF-8
Content-Length: 4testConnection closed by foreign host.
ã ããããããã
ããããä½æ³çã«
ãæå¾ã¯ return $response; ãã£ã¦ã®ãå¾¹åºããã»ããå®å
¨ãããããã ãã¯ãResponseãä¸å¤ã¤ã³ã¹ã¿ã³ã¹ãããªããã°ããã£ãã®ã«ãã¨ãæãç¬éãã¾ããSlimãããå®ç¾©ãã訳ã§ããªãããããããã
(å®ã¯ã便å©ã¡ã½ãããresponseã«çãã¦ã¾ããå¾è¿°ããwriteã¡ã½ãã)ã
The Response Headersã
$headers = $response->getHeaders();
ã¨ãããããããããã°ã®æèãã¨ãã解æã®æèãã¨ãããã¹ãã®æèãã¨ããã§ã便å©ãããªæ°ãããã
便å©ã§ã¯ããã¨æãããã®ã§ãã»ãã®ãã¨è¨æ¶ãã¦ããã¾ããã
ã¾ããã¨ã¯
$headerValueArray = $response->getHeader('Vary'); $headerValueString = $response->getHeaderLine('Vary'); if ($response->hasHeader('Vary')) {
ã¨ããã£ã¦
$newResponse = $oldResponse->withHeader('Content-type', 'application/json'); $newResponse = $oldResponse->withAddedHeader('Allow', 'PUT');
ã¨ãããã®ã§ãããâ¦â¦ãªãã ããï¼ç¹ã
ä¸ã¤ã¯ãæ»ãå¤ãã¤ã³ã¹ã¿ã³ã¹ã§ãããã£ã¦ç¹ããä¸å¤ã¤ã³ã¹ã¿ã³ã¹ã ã£ãããããã£ã¦ã®ãå¿ããã¨è²ã
ã¨åãªãæ°ãããã®ã§ã注æã
ããä¸ã¤ã¯ãwithHeaderã¨withAddedHeaderã®éããã
ã©ã¡ãã vendor/slim/slim/Slim/Http/Message.php ã«å®è£
ããã£ã¦ããã¤
public function withAddedHeader($name, $value) { $clone = clone $this; $clone->headers->add($name, $value); return $clone; }
ã¨ãã£ã¦å®è£
ãªã®ã§ãæ¬å½ã®å®è£
ã¯
vendor/slim/slim/Slim/Http/Headers.php
public function add($key, $value) { $oldValues = $this->get($key, []); $newValues = is_array($value) ? $value : [$value]; $this->set($key, array_merge($oldValues, array_values($newValues))); }
public function set($key, $value) { if (!is_array($value)) { $value = [$value]; } parent::set($this->normalizeKey($key), [ 'value' => $value, 'originalKey' => $key ]); }
ãã®è¾ºã
ãè¤æ°è¡ãã£ã¦å²ã¨(æ¥åã§ä»ä¸ããheaderçã«ã¯)çããã¨æãã®ã§ããåºæ¬ã¯ withHeader() ãã£ã¦è¦ãã¦ããã¦ããããªã«åé¡ãªãããããªãããªãã
ãããã¡ãã
Remove Header
$newResponse = $oldResponse->withoutHeader('Allow');
ãããã£ã¦ãã®ã§ãé©å®ãããªã«ã
ãããä¸å¤ã¤ã³ã¹ã¿ã³ã¹ä»¥ä¸ç¥ã
The Response Body ã«ã¤ãã¦ã¯ã
$body = $response->getBody();
ã§ã¨ããâ¦â¦ã®ã¯ãbodyã¤ã³ã¹ã¿ã³ã¹ãããã©ã¯ vendor/slim/slim/Slim/Http/Body.php ããã
ãªã®ã§
$response->getBody()->write();
ãã®è¾ºããã¾ããã使ãã¨ããããªããã¨ã
ä¸ã«ãæ¸ãããã©ãããã®æ»ãå¤ãstringããªã®ã§ãè²ã
ã¨æ³¨æãã§ã¯ããã
Returning JSONã
â¦â¦â¦ããããã¦ãæãã
vendor/slim/slim/Slim/Http/Response.php
public function withJson($data, $status = null, $encodingOptions = 0) { $response = $this->withBody(new Body(fopen('php://temp', 'r+'))); $response->body->write($json = json_encode($data, $encodingOptions)); // Ensure that the json encoding passed successfully if ($json === false) { throw new \RuntimeException(json_last_error_msg(), json_last_error()); } $responseWithJson = $response->withHeader('Content-Type', 'application/json;charset=utf-8'); if (isset($status)) { return $responseWithJson->withStatus($status); } return $responseWithJson; }
便å©ãã
â¦â¦â¦ãã£ã¡ã¯ãreturn $response->withJson($data);ãã£ã¦ãã£ã¦ãããã®ãâ¦â¦â¦å¾®å¦ã«é¢åï½
ã¾ããwithã¤ãã¦ãããcloneã¤ã³ã¹ã¿ã³ã¹ãè¿ã£ã¦ãããã£ã¦è¦ãã¦ããããã ãã©ããã
ã¨ã¯ãããããã«ãã¦ããjson returnãèæ
®ããã¦ããã®ã¯ããããã£ãã
â¦â¦ã§ãä½æ°ãªãã¿ã¦ããã
vendor/slim/slim/Slim/Http/Response.php
public function write($data) { $this->getBody()->write($data); return $this; }
ãã ã便å©ãªã®ãããããªãã§ããã
ã£ã¦ãã¨ã¯
return $response->write(HTMLæåå);
ã§ããã¾ããªãã
ãã£ã¡ãã£ã¡ã使ãã®ããã£ã¡ã
Returning a Redirectã
ãããããã大äºã
return $response->withRedirect('/new-url', 301);
ã·ã³ãã«ã
â¦â¦â¦Slimã£ã¦ãã«ã¼ãã£ã³ã°ã«ãååä»ããã£ã¦ã§ãããã ã£ããï¼
https://www.slimframework.com/docs/v3/objects/router.html#route-names
ã«åºã¦ããã£ã½ãã®ã§ã次åã«åããï½
ãããã
å²ã¨ã·ã³ãã«ã ã£ããªãâ¦â¦â¦ãããããCookieå¨ãã®ãã¨ã¯ãªããæ¸ãã¦ãªãï½
ã¾ããä¸éãæãè¾¼ãã ä¸ã§ããªãã£ãããæ¹ãã¦èå¯ãã¾ããã