Welcome to Abdul Malik Ikhsan's Blog

23 Responses

Subscribe to comments with RSS.

  1. Griffin Hyde said, on October 30, 2012 at 6:11 pm

    It’s also great to hear that the module creation documentation is about to get some updates. Hopefully it will help the community to create more modules in the future.

  2. engineersexperience said, on November 22, 2013 at 6:21 pm

    Hi Sam,

    Thanks for writting tutorials. Just I started learning ZF2. I followed many of tutorials. Your tutorials helped me to learn quickly and saved lot time ot me… Really you are writing great tutorials for beginners.

    When I use this tutorial, I got error and solved my self.

    Just want to notice it here. So it may be useful for others.

    When trying to implement BootstrapListenerInterface myself results in
    a “Declaration of Application\Module::onBootstrap() must be compatible”

    #. Add “use Zend\EventManager\EventInterface;”

    #. Change:
    public function onBootstrap($e)

    public function onBootstrap(EventInterface $e)

    • samsonasik said, on November 22, 2013 at 9:02 pm

      you’re not follow my post, see I use MvcEvent on there

    • namjitharavind said, on April 2, 2014 at 6:02 pm

      remove BootstrapListenerInterface.

      • Quyền Hồng Minh said, on September 29, 2014 at 4:50 pm

        The first time I follow samsonasik but its not working and after I follow namjitharavind and its still not working for me.Please help me solved this problem.

        in global.php file

        return array(
        ‘service_manager’ => array(
        ‘factories’ => array(
        ‘Zend\Log\Logger’ => function($sm){
        $logger = new Zend\Log\Logger;
        $writer = new Zend\Log\Writer\Stream(‘./data/log/’.date(‘Y-m-d’).’-error.log’);


        return $logger;

        And in Module.php file

        namespace Main;

        use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
        use Zend\ModuleManager\Feature\ConfigProviderInterface;
        use Zend\Mvc\ModuleRouteListener;
        use Zend\Mvc\MvcEvent;

        class Module implements
        AutoloaderProviderInterface, ConfigProviderInterface {

        public function onBootstrap(MvcEvent $e) {

        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();

        $application = $e->getApplication();
        $sm = $application->getServiceManager();
        $sharedManager = $application->getEventManager()->getSharedManager();

        $sharedManager->attach(‘Zend\Mvc\Application’, ‘dispatch.error’, function($e) use ($sm) {
        if ($e->getParam(‘exception’)) {

  3. engineersexperience said, on November 22, 2013 at 6:26 pm

    I think, here we are loggin only critical error i.e. exceptions. If I want catch other issues, how do i do that? e.g. PHP errors.

    EMERG = 0; // Emergency: system is unusable
    ALERT = 1; // Alert: action must be taken immediately
    CRIT = 2; // Critical: critical conditions
    ERR = 3; // Error: error conditions
    WARN = 4; // Warning: warning conditions
    NOTICE = 5; // Notice: normal but significant condition
    INFO = 6; // Informational: informational messages
    DEBUG = 7; // Debug: debug messages

    Will it be possible to catch all above this things?

    Please guide me.

  4. Nicolas Colbert said, on January 11, 2014 at 12:58 am

    I would be great to expand the example to include \Zend\Log\Writer\Mail … that is where I am stuck?

  5. sunil hk said, on February 18, 2014 at 12:14 pm

    hi… i’m tring to store the DB exception into file using the following code.


    array(‘namespaces’ =>
    array(__NAMESPACE__ => __DIR__ . ‘/src/’ . __NAMESPACE__,),

    public function getConfig()
    return include __DIR__ . ‘/config/module.config.php’;
    public function getServiceConfig()
    return array(
    ‘factories’ => array(
    ‘Application\Model\ApplicationTable’ => function($sm) {

    $sm->addInitializer(function ($instance) use ($sm) {
    if ($instance instanceof ServiceLocatorAwareInterface) {
    $dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);
    $table = new \Application\Model\ApplicationTable($dbAdapter);
    return $table;


    return array(
    ‘factories’ => array(
    ‘ApplicationServiceErrorHandling’ => function($sm) {
    $logger = $sm->get(‘Zend\Logs’);
    $service = new ErrorHandlingService($logger);
    return $service;
    ‘Zend\Log’ => function ($sm) {
    $dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);

    $log = new Zend\Log\Logger($dbAdapter);
    $ss = ‘log_’ . date(‘F’) . ‘.txt’;
    $log = new Logger();
    $writer = new LogWriterStream(‘./data/logs/’ . $ss);
    return $log;


    public function onBootstrap($e)
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach(‘dispatch.error’, function($event){
    $exception = $event->getResult()->exception;
    if ($exception) {
    $sm = $event->getApplication()->getServiceManager();
    $service = $sm->get(‘Application\Service\ErrorHandling’);

    —I’m geting error like—-

    Fatal error: Uncaught exception ‘Zend\ServiceManager\Exception\ServiceNotFoundException’ with message ‘Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Application\Service\ErrorHandling’ in /var/www/sun/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:518 Stack trace: #0 /var/www/sun/module/Application/Module.php(87): Zend\ServiceManager\ServiceManager->get(‘Application\Ser…’) #1 [internal function]: Application\{closure}(Object(Zend\Mvc\MvcEvent)) #2 /var/www/sun/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Object(Closure), Object(Zend\Mvc\MvcEvent)) #3 /var/www/sun/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners(‘dispatch.error’, Object(Zend\Mvc\MvcEvent), Array) #4 /var/www/sun/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(120): Zend\EventManager\EventManager->trigger(‘dispatch.error’, in /var/www/sun/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 518

    • samsonasik said, on February 19, 2014 at 2:55 am

      read the error please! and see the different of your registered service and service that you call :p

      • sunil hk said, on February 19, 2014 at 8:44 pm


  6. Sorpea said, on March 4, 2014 at 7:03 pm

    Dear Samsonasik, I have been following your tutorials. All are great, but with this one I got a blank page, the cause is because the Module class implements those three interface classes. After remove those implements I got page as normal. Can you please help me fix it, for the rest of the code I did follow your tutorial literaly. How can I implement those interfaces without error?

    • ramon said, on September 23, 2014 at 11:53 am

      I got it now.. Thanks..nice tutorial

  7. William Zimmermann said, on November 14, 2014 at 8:18 pm

    Thank you very much.

  8. Felipe Ramirez said, on October 2, 2015 at 10:42 pm

    how can i set zf1 “$logger->setEventItem(‘remote’, $_SERVER[‘REMOTE_ADDR’])”
    for zf2?

    my mapping:

    $columnMapping = array(

  9. Felipe Ramirez said, on October 3, 2015 at 2:30 am

    i solved:

    $mapping = array(
    ‘timestamp’ => ‘timestamp’,
    ‘priority’ => ‘priority’,
    ‘message’ => ‘message’,
    ‘extra’ =>array(
    ‘username’ => ‘username’,
    ‘remote’ => ‘remote’,
    ‘username’ => ‘username’,
    ‘idservicio’ => ‘idservicio’,

    $request = new \Zend\Http\PhpEnvironment\Request();
    $httpHost = $request->getServer(‘HTTP_HOST’);
    $remoteAddr = $request->getServer(‘REMOTE_ADDR’);

    $extra = array(‘username’=>’framirez’, ‘remote’=>$remoteAddr);
    $this->setLog()->info(“test”, $extra);

  10. Dmitry said, on November 2, 2015 at 2:46 am

    Hey Abdul. Thanks for the post. I have something to add. This error handler should be assigned not only to “dispatch.error” but to “render.error”. For example, errors in view helpers won’t be logged.

    • samsonasik said, on November 2, 2015 at 3:15 am

      thanks😉, this is just an example😉, of course, you can add that.

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: