Rest 例外策略不';不要与JsonStrategy合作

Rest 例外策略不';不要与JsonStrategy合作,rest,zend-framework2,Rest,Zend Framework2,我在这里报告了一个问题()。有一个解决方案,但我不知道在哪里实施。有人能帮忙吗 关于这一点,这里是一个JsonExceptionStrategy的代码——把它放在某个地方,比如模块中 namespace YourNamespace; use Zend\EventManager\EventManagerInterface; use Zend\Mvc\Application; use Zend\Mvc\View\Http\ExceptionStrategy; use Zend\Mvc\MvcEve

我在这里报告了一个问题()。有一个解决方案,但我不知道在哪里实施。有人能帮忙吗


关于这一点,

这里是一个JsonExceptionStrategy的代码——把它放在某个地方,比如模块中

namespace YourNamespace;

use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Application;
use Zend\Mvc\View\Http\ExceptionStrategy;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\JsonModel;

/**
 *
 * @since   1.0
 * @author  Tim Roediger <superdweebie@gmail.com>
 */
class JsonExceptionStrategy extends ExceptionStrategy
{

    /**
     * Attach the aggregate to the specified event manager
     *
     * @param  EventManagerInterface $events
     * @return void
     */
    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareExceptionViewModel'), 100);
    }

    /**
     * Create an exception json view model, and set the HTTP status code
     *
     * @todo   dispatch.error does not halt dispatch unless a response is
     *         returned. As such, we likely need to trigger rendering as a low
     *         priority dispatch.error event (or goto a render event) to ensure
     *         rendering occurs, and that munging of view models occurs when
     *         expected.
     * @param  MvcEvent $e
     * @return void
     */
    public function prepareExceptionViewModel(MvcEvent $e)
    {
        // Do nothing if no error in the event
        $error = $e->getError();
        if (empty($error)) {
            return;
        }

        // Do nothing if the result is a response object
        $result = $e->getResult();
        if ($result instanceof Response) {
            return;
        }

        switch ($error) {
            case Application::ERROR_CONTROLLER_NOT_FOUND:
            case Application::ERROR_CONTROLLER_INVALID:
            case Application::ERROR_ROUTER_NO_MATCH:
                // Specifically not handling these
                return;

            case Application::ERROR_EXCEPTION:
            default:
                $exception = $e->getParam('exception');
                $modelData = array(
                    'message' => $exception->getMessage(),
                    'type' => get_class($exception)
                );

                if ($this->displayExceptions()){
                    $modelData['exception'] = $exception;
                }
                $e->setResult(new JsonModel($modelData));
                $e->setError(false);

                $response = $e->getResponse();
                if (!$response) {
                    $response = new HttpResponse();
                    $e->setResponse($response);
                }
                $response->setStatusCode(500);
                break;
        }
    }
}

你可以走了

由于该问题的状态是
已解决
您最好在github上打开另一个问题,因为显然还有另一个问题。请确保附加引发问题的代码。是否有任何方法可以更改控制器操作中的ExecutionStrategy?
/**
 *
 * @param \Zend\EventManager\Event $event
 */
public function onBootstrap(MvcEvent $event)
{

    $application = $event->getTarget();
    $serviceManager = $application->getServiceManager();
    $config = $serviceManager->get('Config');

    // Config json enabled exceptionStrategy
    $exceptionStrategy = new JsonExceptionStrategy();

    $displayExceptions = false;

    if (isset($config['view_manager']['display_exceptions'])) {
        $displayExceptions = $config['view_manager']['display_exceptions'];
    }

    $exceptionStrategy->setDisplayExceptions($displayExceptions);
    $exceptionStrategy->attach($application->getEventManager());
}