错误处理


Slim 框架有个错误处理去接收PHP异常错误。错误处理通常接收当前的HTTP请求和响应对象。错误处理会返回一个恰当的响应对象给客户端。

默认的错误处理

Slim 默认的错误处理是非常基础的。它会设置响应状态为500,输出类型为 text/html,并返回普通的错误消息给客户端。

这可能并不是很适应生产应用。所以非常鼓励你实现自己的错误处理。

默认的错误处理也包含错误的详细信息,但是需要你去设置 displayErrorDetails 为真。

$configuration = [
'settings' => [
    'displayErrorDetails' => true,
],
];
$c = new \Slim\Container($configuration);
$app = new \Slim\App($c);

自定义错误处理

Slim 应用的错误处理是一个 Pimple 的服务,你可通过应用容器自定义一个 Pimple 工厂方法把你自己的错误处理替代默认的错误处理。

有两种方式注入错误处理

在创建应用之前
$c = new \Slim\Container();
$c['errorHandler'] = function ($c) {
		return function ($request, $response, $exception) use ($c) {
				return $c['response']->withStatus(500)
														 ->withHeader('Content-Type', 'text/html')
														 ->write('Something went wrong!');
		};
};
$app = new \Slim\App($c);
在创建应用之后
$app = new \Slim\App();
$c = $app->getContainer();
$c['errorHandler'] = function ($c) {
		return function ($request, $response, $exception) use ($c) {
				return $c['response']->withStatus(500)
														 ->withHeader('Content-Type', 'text/html')
														 ->write('Something went wrong!');
		};
};

在这个例子中,我们定义一个 errorHandler 工厂返回一个回调。返回的回调接收三个参数:

1. \Psr\Http\Message\ServerRequestInterface 的实例
2. \Psr\Http\Message\ResponseInterface 的实例
3. \Exception 的实例

回调__必须__返回一个 \Psr\Http\Message\ResponseInterface 的实现作为合适的异常报错处理。

屏蔽错误信息

屏蔽Slim的错误处理,只要从容器里把错误处理移除就可以了:

unset($app->getContainer()['errorHandler']);

现在在你的Slim应用中出现任何错误,Slim都不会处理了

404 Not Found 错误处理


如果你的Slim应用中没有任何的路由可以匹配当前请求的URI,Slim应用就会调用 Not Found 错误处理返回一个 HTTP/1.1 404 Not Found 响应给客户端。

默认的Not Found错误处理

任何一个Slim应用都有404 Not Found错误处理。这个错误处理会设置响应状态为404,响应输出类型为 test/html 和一个简单的解释返回给客户端。

自定义Not Found 错误处理

Slim 应用的Not Found 错误处理是一个 Pimple 的服务,你可通过应用容器自定义一个 Pimple 工厂方法把你自己的Not Found 错误处理替代默认的Not Found 错误处理。

$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

在这个例子中,我们定义一个 notFoundHandler工厂返回一个回调。返回的回调接收两个参数:

1. \Psr\Http\Message\ServerRequestInterface 的实例
2. \Psr\Http\Message\ResponseInterface 的实例

这个回调必须返回一个 \Psr\Http\Message\ResponseInterface 的实现。

405 Not Allowed 错误处理


如果你的Slim应用中有路由可以匹配当前请求的URI但是不是HTTP请求方法,Slim应用就会调用 Not Allowed 错误处理返回一个 HTTP/1.1 405 Not Allowed 响应给客户端。

默认的Not Allowed错误处理

任何一个Slim应用都有405 Not Allowed错误处理。这个错误处理会设置响应状态为405,响应输出类型为 test/html, 还会添加一个 Allowed的HTTP头值为允许的HTTP方法 和一个简单的解释返回给客户端。

自定义Not Found 错误处理

Slim 应用的405 Not Allowed错误处理是一个 Pimple 的服务,你可通过应用容器自定义一个 Pimple 工厂方法把你自己的405 Not Allowed 错误处理替代默认的405 Not Allowed错误处理。

// 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));
		};
};

在这个例子中,我们定义一个 notAllowedHandler工厂返回一个回调。返回的回调接收三个参数:

1. \Psr\Http\Message\ServerRequestInterface 的实例
2. \Psr\Http\Message\ResponseInterface 的实例
3. 一个被允许的方法组成的数字索引数组

这个回调必须返回一个 \Psr\Http\Message\ResponseInterface 的实现。


文档翻译的并不是很专业,仅供个人学习作用,建议看官方文档。

如有兴趣,请看下一篇 SLIM 3 文档(十一)-烹饪书5)