2

ここで報告された問題があります ( http://framework.zend.com/issues/browse/ZF2-379?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab#issue -タブ)。解決策はありますが、それを実装する場所がわかりません。誰でも助けることができますか?

よろしく、

4

1 に答える 1

3

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;
        }
    }
}

次に、Module.php に以下を追加します。

/**
 *
 * @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());
}

準備万端です!

于 2013-02-02T10:13:15.720 に答える