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

时间:2016-05-20 15:32:36

标签: php error-handling slim-3

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

<label for="first_name">First name:
    <span  v-if="$validation1.first_name.required" class="invalid">Enter your first name.</span>                            
    <input id="first_name" placeholder="e.g. Christopher" class="" v-validate:first_name="[&#039;required&#039;]" v-model="first_name" name="first_name" type="text">
</label>

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

1 个答案:

答案 0 :(得分:1)

我看到自定义错误在容器中。打电话吧。但我认为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);
});