ZF2 : disable error page layout

2019-07-24 01:18发布

问题:

im trying to disable layout while showing error/exception page in my zf2 module

but nothing works

please help

回答1:

Final Solution in my Module.php

$eventManager = $e->getApplication()->getEventManager();



        $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($e) {
             $result = $e->getResult();
             $result->setTerminal(TRUE);

            });

it works and only loads error/404 view file not the layout file

thanks andrew



回答2:

If you look at the Zend Framework 2 MVC module you will see possibilities for this..

DispatchListener.php

    try {
        $return = $controller->dispatch($request, $response);
    } catch (\Exception $ex) {
        $e->setError($application::ERROR_EXCEPTION)
              ->setController($controllerName)
              ->setControllerClass(get_class($controller))
              ->setParam('exception', $ex);
        // look here...
        $results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
        $return = $results->last();
        if (! $return) {
            $return = $e->getResult();
        }
    }

You can see they MvcEvent which is triggered when you have an exception thrown inside the controller, there's a few other processes attaching to this event.

You could attach a method to this event and do what ever you want.

As an example look at ExceptionStrategy.php

public function prepareExceptionViewModel(MvcEvent $e)
{
     ....
}


回答3:

this is not your ans but it will help to other in zf2

 public function indexAction()
    {    
        echo "json"
        return $this->getResponse();
    }


回答4:

The easiest way is to use config config/autoload/local.php

return array(
    'view_manager' => array(
        'display_exceptions' => false
    )
);

Adding this lines disables exceptions. In addition you can use your own local.php on a dev server.