how to add custom library in your zend framework 2 application

Oct 22, 2012   //   by kdecom   //   custom library, zend framewok 2, ZEND FRAMEWORK, zend framework tutorial  //  4 Comments
Zend Framework 2 and Custom Library auto load
Zend Framework 2 And Custom Library

I know most people having some sort of library code in their application or website…

In Zend Framework 1 it was easy just to add a namespace in config file and it does autoload all your files.
Well in Zend Framework 2 you can have a custom library and you can do autoload as well but to be honest i am not impressed with the amount of work that you need to do with Zend Framework 2. I am sure there is some sort of though behind that but i am not sure now.

Now lets start with making a custom library … Let just say you want to add a log of visior of every visit on your application or website.

Easiest way to do is just add a custom controller and in init method you can call something like that..

$this->logVisitor(); which saves the visitor information into a log_visitor table.

Now lets start some code…

Step 1:- Create a custom Library in your vendor Folder.. Image shows how the folder structure looks like it in my project. You can have different stucture but its up to you and beased on your folder structure you have to adjust some path in your code.

Step 2 :-

Change your controller extended class

open your IndexController.php

And replace the code something like this way.

namespace ApplicationController;
use KdecomMvcController;
// Changed Line
//use ZendMvcControllerAbstractActionController; –Comment this line
use ZendViewModelViewModel;
use ZendEventManagerEventManager;
use ZendEventManagerEvent;

class IndexController extends Controller{     // Change this line

    public function indexAction() {
        return new ViewModel(array(
            ‘title’ => ‘Hello World’,
        ));
    }

}

Now next step is add some code into our custom library.

First of all open a file ./vendor/Kdecom/autoload_classmap.php

return array(
    ‘KdecomModule’ => __DIR__ . ‘/Module.php’,
    ‘KdecomMvc’ => __DIR__ . ‘/library/Kdecom/Mvc/Controller.php’,
)

Next is
./vendor/Kdecom/Module.php

namespace Kdecom;

use ZendModuleManagerFeatureAutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            ‘ZendLoaderClassMapAutoloader’ => array(
                __DIR__ . ‘/autoload_classmap.php’,
            ),
            ‘ZendLoaderStandardAutoloader’ => array(
                ‘namespaces’ => array(
                    __NAMESPACE__ => __DIR__ . ‘/library/’ . __NAMESPACE__,
                ),
            ),
        );
    }
}

Now inside your library create a file inside a folder:-

 ./vendor/Kdecom/library/Kdecom/Mvc/Controller.php

Paste this code into your Controller.php file.

namespace KdecomMvc;

use ZendMvcControllerAbstractActionController;

class Controller extends AbstractActionController {

    public function __construct() {
        $this->logVisitor();
    }

    public function logVisitor() {
        // save the log into into Database here..
    }
}

One final thing that we need to do which is add this library into your application.config.php file.
So your application.config.php file will looks something like this way…

return array(
    ‘modules’ => array(
        ‘Application’,
        ‘Kdecom’    ),
    ‘module_listener_options’ => array(
        ‘config_glob_paths’    => array(
            ‘config/autoload/{,*.}{global,local}.php’,
        ),
        ‘module_paths’ => array(
            ‘./module’,
            ‘./vendor’,
        ),
    ),
);

4 Comments

  • Thanks,

    this is really very useful answer,
    if you have any more other stuff then please post it.

    • thanks…

      Yes i am working on some more blogs… So keep visiting

  • This is exactly what I was looking for. Magic Autoloader, composer, etc. are all great but I just wanted to include my normal personal library like I did with ZF1. This does it. Thanks!

    • It works. but still its not same as ZF1. When you just say in your config file autoloadernamespaces.kdecom = “Kdecom_” it does auto load all the file.
      So my solution is not same as that. if you or anyone knows how to make this task simple.

Leave a comment

css.php