Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Create Custom Error Page

Posted in Teknologi, Tutorial PHP, Zend Framework 2 by samsonasik on September 19, 2012

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
    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
        //if this module 
	$exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();

    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 !

14 Responses

Subscribe to comments with RSS.

  1. Gaston Cortes said, on May 2, 2013 at 6:45 am

    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?


    • samsonasik said, on May 3, 2013 at 2:32 am

      i’m not sure i understand what you’ve asked. why setting up exceptionTemplate in table class ?

  2. GRK said, on June 20, 2013 at 7:59 pm

    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\…….”

  3. patrickd28 said, on September 5, 2013 at 1:06 am


    I want to add custom page hover all my app with many modules does your technic apply to me?


    • samsonasik said, on September 5, 2013 at 1:44 am

      a page or a content ? if content, you can do :

      echo $this->render('path/to/render.phtml');
  4. sanjay said, on November 8, 2013 at 2:41 pm

    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?

    • samsonasik said, on November 8, 2013 at 3:26 pm

      read module.config.php under Application module under view_manager key at ZendSkeletonApplication

      • sanjay said, on November 8, 2013 at 5:11 pm

        got idea now🙂

  5. Gytis said, on November 11, 2013 at 9:22 pm

    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!

  6. maidanh said, on March 29, 2014 at 8:23 pm

    hi sam,
    thank for you post. if i want to set layout for error page. how can i do?

    • samsonasik said, on March 30, 2014 at 12:39 am

      you can type :


      at the end of the error page and then you should create your own layout/layout.phtml.

  7. Lumser said, on July 18, 2014 at 7:06 pm

    What about ‘dispatch.error’ event and ‘error-router-no-match’ error , is there a way to create different error page for each module ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: