Zend Framework 2 : Create Custom Error Page
In production Application Environment, we should handle error page that user will be viewed. In Zend Framework 2, we can create our custom error page which show the user that the page is error, but not show them the exception.
Just define in module.config.php in one of your modules :
return array( 'controllers' => array( /* common code */ ), 'router' => array( /* common code */ ), 'view_manager' => array( 'exception_template' => 'error/index', 'template_map' => array( 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), );
How about custom error page in specific module ? This is it :
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 */} }
Place your errorcustom.phtml inside your ModuleName/view/error folder, and create errorcustom.phtml that can be easy as :
Hey, something wrong. we will correct this soon, we promise !
[…] https://samsonasik.wordpress.com/2012/09/19/zend-framework-2-create-custom-error-page/ […]
Hi Abdul thank you for posting this,
I am having some trouble when I want to call from a controller an other module table class, on this module I have the same exceptionTemplate setup but when I trow an exception on the table class I get the template from the controller module setup not from the table class module. Do you have any Idea why is this happening?
Thanks
i’m not sure i understand what you’ve asked. why setting up exceptionTemplate in table class ?
Hi Abdul thank you for posting this,
Am getting Error on same page instead of customerropage
The error is “Notice: Trying to get property of non-object in c:\xampp\…….”
Thanks.
Hi,
I want to add custom page hover all my app with many modules does your technic apply to me?
Thanks.
a page or a content ? if content, you can do :
Hello sam,
Thanks for your post.. it helped me a lot to set up custom 404 page but i am confused about 500(Internal Server Error) error page?
is it the same way i can configure 500 error page?
read module.config.php under Application module under view_manager key at ZendSkeletonApplication
Thanks..
got idea now 🙂
You, Sir, are genius. I was thinking for some time how to create custom 404 pages, and you saved some of my time. Thank you!
you’re welcome 😉
hi sam,
thank for you post. if i want to set layout for error page. how can i do?
you can type :
at the end of the error page and then you should create your own layout/layout.phtml.
What about ‘dispatch.error’ event and ‘error-router-no-match’ error , is there a way to create different error page for each module ?