Slim PSR-7

Programming/Slim 2016. 2. 28. 01:01

Slim 은 Request 와 Response 객체에 대해 PSR-7 인터페이스를 제공한다.

예를 들면, Slim 의 라우트는 \Slim\Http\Response 인스턴스를 반환하지 않고,

\GuzzleHttp\Psr7\CachingStream 의 인스턴스나 \GuzzleHttp\Psr7\stream_for() 함수로부터 전달된 인스턴스를 반환할 수 있다.


Slim 은 자체 PSR-7 인터페이스 구현을 제공하지만, Slim 의 기본 PSR-7 객체를 서드파티 구현으로 대신할 수도 있다.

단지 \Psr\Http\Message\ServerRequestInterface 와 \Psr\Http\Message\ResponseInterface 각각의 인스턴스로 어플리케이션의 request 와 response 서비스를 덮어 씌우면 된다.



Value objects


Slim 의 Request 와 Response 객체는 변하지 않는 Value Object 이다.

이 객체들은 변경된 속성 값을 가진 복제된 버전을 요청하여 변경될 수 있다.

이 때의 처리 시간/메모리(overhead)등은 성능에 영향을 주지 않는다.


우리는 어떤 PSR-7 인터페이스 메소드를 호출하여 값 객체의 복사를 요청할 수 있다.

예를 들면, PSR-7 Response 객체의 withHeader($name, $value) 메소드는 새로운 HTTP header 로 복제된 값 객체를 반환한다.


1
2
3
4
5
6
7
8
9
<?php
$app = new \Slim\App;
$app->get('/foo'function ($req$res$args) {
    return $res->withHeader(
        'Content-Type',
        'application/json'
    );
});
$app->run();
cs



Request 와 Response 객체를 변경하는 PSR-7 인터페이스의 메소드


  • withProtocolVersion($version)
  • withHeader($name, $value)
  • withAddedHeader($name, $value)
  • withoutHeader($name)
  • withBody(StreamInterface $body)


Request 객체를 변경하는 PSR-7 인터페이스의 메소드


  • withMethod($method)
  • withUri(UriInterface $uri, $preserveHost = false)
  • withCookieParams(array $cookies)
  • withQueryParams(array $query)
  • withUploadedFiles(array $uploadedFiles)
  • withParsedBody($data)
  • withAttribute($name, $value)
  • withoutAttribute($name)


Response 객체를 변경하는 PSR-7 인터페이스의 메소드

  • withStatus($code, $reasonPhrase = '')




WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,