404 Not Found Handler


Slim 어플리케이션에 HTTP 요청 URI 와 매칭되는 라우트가 없다면 Not Found Handler 를 호출하고 HTTP 클라이언트에 HTTP/1.1 404 Not Found 응답을 반환한다.


Default Not Found handler

각 Slim 어프릴케이션은 기본 Not Found handler 를 가진다.

이 핸들러는 Response 상태를 404 로, Content type 을 text/html 로 지정하고, Response 본문에 간단한 설명을 작성한다.


Custom Not Found handler

Slim 어플리케이션의 Not Found handler 는 Pimple 서비스이다. 

어플리케이션 컨테이너로 Pimple 의 팩토리 메소드를 사용자 정의하여 이 에러 핸들러를 교체할 수 있다.


$c = new \Slim\Container(); //Create Your container
 
//Override the default Not Found Handler
$c['notFoundHandler'= function ($c) {
    return function ($request$response) use ($c) {
        return $c['response']
            ->withStatus(404)
            ->withHeader('Content-Type''text/html')
            ->write('Page not found');
    };
};
 
//Create Slim
$app = new \Slim\App($c);
 
//... Your code
cs


이 예에서, 우리는 새로운 notFoundHandler 팩토리를 정의하고 호출 가능한 것을 반환한다. 반환된 호출 가능한 것은 두가지 매개변수를 가진다.

  • \Psr\Http\Message\ServerRequestInterface 인스턴스
  • \Psr\Http\Message\ResponseInterface 인스턴스


호출 가능한 것은 적절한 \Psr\Http\Message\ResponseInterface 인스턴스를 반환해야 한다.



405 Not Allowed Handler


Slim 어플리케이션에 HTTP 요청 URI 와 매칭되는 라우트가 존재하지만 HTTP 요청 메소드가 아니라면, Not Allowed Handler 를 호출하고 HTTP 클라이언트에 HTTP/1.1 405 Not Allowed 응답을 반환한다.


Default Not Allowed handler

각 Slim 어프릴케이션은 기본 Not Allowed handler 를 가진다.

이 핸들러는 Response 상태를 405 로, Content type 을 text/html 로 지정하고, 사용 가능한 HTTP 메소드들을 콤마로 구분한 리스트로 HTTP 헤더에 Allowed 를 추가하고, Response 본문에 간단한 설명을 작성한다.


Custom Not Allowed handler

Slim 어플리케이션의 Not Allowed handler 는 Pimple 서비스이다. 

어플리케이션 컨테이너로 Pimple 의 팩토리 메소드를 사용자 정의하여 이 에러 핸들러를 교체할 수 있다.


// Create Slim
$app = new \Slim\App();
// get the app's di-container
$c = $app->getContainer();
$c['notAllowedHandler'= function ($c) {
    return function ($request$response$methods) use ($c) {
        return $c['response']
            ->withStatus(405)
            ->withHeader('Allow', implode(', '$methods))
            ->withHeader('Content-type''text/html')
            ->write('Method must be one of: ' . implode(', '$methods));
    };
};
cs


\Slim\Container 의 새로운 인스턴스를 사용하여 사전 slim 생성 메소드에 대해 Not Found 문서를 확인하라.


이 예에서, 우리는 새로운 notAllowedHandler 팩토리를 정의하고 호출 가능한 것을 반환한다. 반환된 호출 가능한 것은 세가지 매개변수를 가진다.

  • \Psr\Http\Message\ServerRequestInterface 인스턴스
  • \Psr\Http\Message\ResponseInterface 인스턴스
  • 사용 가능한 HTTP 메소드 이름의 숫자 배열


호출 가능한 것은 적절한 \Psr\Http\Message\ResponseInterface 인스턴스를 반환해야 한다.




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

,