Q . How to load custom library in ZF2(Zend Framework 2).

Brad Johnson. asked, Aug 24 ' 2015

Please advice with example.

  • zend-framework2
  • custom
  • library

1 Answers

It's very simple to autoload custom libraries in the zf2 (zend framework 2).

First you create a directory named "Lib" inside the vendor directory.

Now add this path to getAutoloaderConfig of any module.php class in the application.

for e.g

namespace Application;

class Module{
    public function getAutoloaderConfig()
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    'Lib' => __DIR__ . '/../../vendor/Lib'


Now your custom library is autoloaded in your application. you can use your library with the namspace given in the class.

for eg :

namespace Appliaction\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController{

    public function indexAction(){

        $service = new \Lib\Service\Custom();
        echo $service->demo();


Leave a Ansewer