在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/

10-10 18:15