ZF2 : disable error page layout

2019-07-24 00:41发布

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

but nothing works

please help

4条回答
何必那么认真
2楼-- · 2019-07-24 01:17

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)
{
     ....
}
查看更多
倾城 Initia
3楼-- · 2019-07-24 01:17

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.

查看更多
地球回转人心会变
4楼-- · 2019-07-24 01:25

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

查看更多
萌系小妹纸
5楼-- · 2019-07-24 01:33

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

 public function indexAction()
    {    
        echo "json"
        return $this->getResponse();
    }
查看更多
登录 后发表回答