Montag, 26. November 2012

Zend Framework 2 :: Create Custom Library

           Adding custom libraries to Zend Framework 2

I just started getting acquainted with Zend framework 2 and the second problem I have had sofar after creating a database driven module was adding my ZF1 custom libraries that should be available to all modules. I am not impressed with the much work (according to the tutorials I have seen sofar) needed to autoload custom libraries in ZF2.

I have gone through a couple of tutorials treating this topic, but none seem to work well for me. The only option I had was to take a look at composer and understand how it autoloads the ZF2 vendor. After understanding the ready made composer autoload setup, I realized I could solve this problem with just one line of code :

1.) Creating the file Structure for your custom library

- Go to the vendor folder and create your library folder in there. For example, vendor/MyLibrary /library.
- Create another folder under library that will represent your namespace. So now you have the following  structure : vendor/MyLibrary/library/Utils.
- Add another folder Mvc/models or call it what you want to Utils that holds your classes.
- Create a controller and call it what you want in Mvc

Your controller should look like this :

namespace Utils\Mvc;
use Utils\Mvc\Models\ModelResult
use Zend\Mvc\Controller\AbstractActionController;

class UtilsController extends AbstractActionController
{
    public function __construct()
   {
       DO_Some_Work
    }
    public function doSomething()
   {
        //instantiate your model here and return result
     $result = Your_Model_Here();
     return $result';
    }
}

Note that this controller extends AbstractActionController so in your Module's controller, you'll have to extend this controller. Your Module's controller :

namespace Admin\Controller;
use Utils\Mvc\UtilsController;
//use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends UtilsController
{
    public function indexAction()
    {
       
        $getCustomeControllerResult =  $this->doSomething();
    
        return new ViewModel();
    }
    public function addUserAction(){
         return new ViewModel();
    }

}

 * You may as well call the custom controller in the init function instead of index.
You'll have some error messages if you run your code at this time.

2.) Adding a single line to composers autoload_namespaces.php

To finalize everything,
- go to the composer folder and open autoload_namespaces.php
- add your namespace to the returned array and you are done

$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);

return array(
    'Zend\\' => $vendorDir . '/zendframework/zendframework/library/',
    'ZendTest\\' => $vendorDir . '/zendframework/zendframework/tests/',
    'Utils\\' => $vendorDir . '/MyLibrary/library/',
);

I know some of you are pulling your hairs right now and asking what about if I auto update composer ??
Not a big deal for me, I backup my working copy so each time I update I'll just have to copy paste the lines I added to the returned array. For my project, I wouldn't need to update composer anymore.

2 Kommentare:

  1. Just add an autoload key in your main composer.json:
    ...
    "autoload": {
    "psr-0": {"Your": "vendor/Your/library"}
    },
    ...

    and run

    #composer update

    your library will be added to vendor/autoload

    AntwortenLöschen
  2. Nice! This post is really useful for me and everyone. Thanks for sharing the awesome zend framework code with article.

    Hire Ruby on Rail Developers

    AntwortenLöschen