php - Slim 3 激活自定义错误处理程序

标签 php error-handling slim-3

在 Slim 3 中,这是一个在应用程序中注入(inject)的自定义错误处理程序的示例:

$container = new \Slim\Container();
$container['customError'] = function($c){
    return function ($request, $response) use ($c) {
        $output = ['success'=>0, 'error'=>"Custom Error Output."];
        return $c['response']
            ->withStatus(400)
            ->withHeader('Content-Type', 'application/json')
            ->write(json_encode($output));
    };
};
$app = new \Slim\App($container);

我的问题是,如何触发此自定义错误?

最佳答案

我看到自定义错误在容器中。就叫吧。但我认为你不需要use($c)return function ($request, $response) use ($c) { .

这是示例代码:

<?php

$container = new \Slim\Container();

$container['customError'] = function($c){
    return function ($request, $response) {
        $output = ['success'=>0, 'error'=>"Custom Error Output."];
        return $response
            ->withStatus(400)
            ->withHeader('Content-Type', 'application/json')
            ->write(json_encode($output));
    };
};

// init
$app = new \Slim\App($container);

// route
$app->get('/error-page', function ($request, $response, $args) {
    $customError = $this->get('customError');
    return $customError($request, $response);
});

关于php - Slim 3 激活自定义错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37350442/

相关文章:

bash - Bash Jobs : how can I output job process information along with errors?

php - 在 Slim Framework 上重定向错误

php - Slim 3 如何处理与 MySql 的多个连接

php - 如何返回内连接查询结果

php - 前端后端的 url_for - Symfony

php - Laravel 5 LEFT JOIN 的 id 问题

php - Slim 3 getParsedBody() 始终为 null 和空

php - 如何使用 php 脚本可读的 wininet 传输文件?

error-handling - 来自 Jersey 的Dropwizard错误消息

image-processing - Photoshop/InDesign CS5 脚本 : image size conversion error is using too much memory?