Reputation: 413
My question is regarding customizing how errors are handled in Zend 2.
Suppose I'd like to customize the layout such that I want to do this in an action in my controller:
$layout = $this->layout();
$myNav = new ViewModel(array('nav' => $this->getNav());
$myNav->setTemplate('layout/nav');
$layout->addChild($myNav, 'navigation');
Works great when I place this into my controller for regular (i.e. non-404) viewing. Now I've customized my layout so that I can do <?php echo $this->navigation; ?>
and the layout/nav.phtml
is fired up and everything works just hunky dory.
Now, suppose I want to do the exact same thing when errors are rendered. I need to be able to inject the above code somehow prior to the error handler returning it's own ViewModel(...)
into the error/404.phtml
template.
How do you do that?
I suspect that it's something like setting up the correct class for the service manager like this in module.config.php
:
'service_manager' => array(
'services' => array(
'error_handler' => 'MyModule\Controller\MyCustomErrorController'
//and so on...
How do I do this?
UPDATE:
In my Module.php
I've attached a method for MvcEvent::EVENT_DISPATCH_ERROR
. Variant A works, Variant B does not. So you can't use partials here?? Am I missing something really basic??
Variant A
public function onDispatchError(MvcEvent $event)
{
$sm = $event->getApplication()->getServiceManager();
$vm = $event->getViewModel();
$vm->setVariable('nav', '<h1>test do i work?</h1>');
//Works
}
Variant B
public function onDispatchError(MvcEvent $event)
{
$sm = $event->getApplication()->getServiceManager();
$vm = $event->getViewModel();
$nav = new ViewModel(array('test'=>'hello there'));
$nav->setTemplate('layout/simpletest');//contents: <?php echo $this->test; ?>
$vm->addChild($nav, 'nav');
//In the template, <?php echo $this->nav; ?> has nothing...
}
Upvotes: 1
Views: 2450
Reputation: 12809
You can attach to an even to handle what happend when a 404 is triggered:
Module.php
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/**
* Log any Uncaught Errors
*/
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
function($e) use ($sm) {
/**
* Decide what to do now we've got a problem...
* Log the issue etc..
* You could forward to some custom controller if you wanted..
*/
//$sm->get('Zend\Log\Logger')->crit('an error occurred... bla');
$controller = $e->getTarget();
//$routeMatch = $e->getRouteMatch();
$controller->layout('somelayout'); // possibly change the layout..
}
);
}
Upvotes: 0
Reputation: 443
Zf2 use module.config.php file to set error handling:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
This should handle 4xx client errors and 5xx server errors.
For custom error page in specific module.
namespace ModuleName;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;
class Module implements
BootstrapListenerInterface,
AutoloaderProviderInterface,
ConfigProviderInterface
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration' ), 100);
}
public function loadConfiguration(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$controller = $e->getRouteMatch()->getParam('controller');
if (0 !== strpos($controller, __NAMESPACE__, 0)) {
//if not this module
return;
}
//if this module
$exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
$exceptionstrategy->setExceptionTemplate('error/errorcustom');
}
public function getAutoloaderConfig(){ /* common code */ }
public function getConfig(){ /* common code */}
}
The solution is provided by "samsonasik" from http://samsonasik.wordpress.com/2012/09/19/zend-framework-2-create-custom-error-page/
Upvotes: 1