无法使用zf2

时间:2015-08-31 09:49:45

标签: php api zend-framework2 apigility

我是zendframework的新手。我正在使用apigility进行休息服务,而ApiProblemListner则在发生任何错误时返回响应。

我在模型中有一个函数,这个函数只是通过使用php异常在catch块中使用的异常

我使用模型函数作为控制器中的实用函数来捕获那些异常。在捕捉我正在使用的异常时

try{
   imageUploade(); // this function in model and throwing exception if any error
}catch(\Exception $e){
   return new ApiProblemResponse(new ApiProblem(500 , $e->getMessage()));
}

如果imageUploade()如果图像大小更多则抛出异常,那么我能够捕获catch块中的异常。我尝试echo $e->getMessage();并打印异常bt,如果我使用new ApiProblem(500 , $e->getMessage())它不会使用500消息重新调整json错误响应。它什么也没有回来。即使它没有显示任何错误。

似乎无法使用此类呈现错误。我不确定是否需要添加任何事件。

我试图搜索文档但无法找到它。

提前致谢。

1 个答案:

答案 0 :(得分:1)

通常,如果从控制器操作直接返回ApiProblemResponse,它应该可以正常工作。
你确定您的Api-Problem模块是否有效?

尝试这样一次:

<?php

Application\Namespace;

use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;

class IndexController
{
    /**
     * @return ApiProblemResponse
     */
    public function indexAction()
    {
        return new ApiProblemResponse(new ApiProblem(500, 'test'));
    }
}

如果这不起作用,那么我认为您的Api-Problem模块没有运行或者异常从未被捕获。

相关问题