概览
Things go wrong. You can’t predict errors, but you can anticipate them. Each Slim Framework application has an error handler that receives all uncaught PHP exceptions. This error handler also receives the current HTTP request and response objects, too. The error handler must prepare and return an appropriate Response object to be returned to the HTTP client.
默认错误处理
The default error handler is very basic. It sets the Response status code to 500
, it sets the Response content type to text/html
, and it appends error diagnostic information to the Response body.
This is probably not appropriate for production applications. You are strongly encouraged to implement your own Slim application error handler.
自定义错误处理
A Slim Framework application’s error handler is a Pimple service. You can substitute your own error handler by defining a custom Pimple factory method with the application container.
$app['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
return $c['response']->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('Something went wrong!');
};
};
In this example, we define a new errorHandler
factory that returns a callable. The returned callable accepts three arguments:
- A
\Psr\Http\Message\RequestInterface
instance - A
\Psr\Http\Message\ResponseInterface
instance - A
\Exception
instance
The callable MUST return a new \Psr\Http\Message\ResponseInterface
instance as is appropriate for the given exception.