Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zendframework 2 - where to save custom library folder

In Zendframework-1, we usually save the customized code under library folder (parallel to application folder) almost using the same folder structure as zend framework (vendor) library to create plugin or extend core library.

In zend framework 2, folder structure is changed. zend vendor core library is moved under Vendor folder and application folder is moved into Module (root) folder.

My question is, which is the best place to save customized plugin/code based library folder in ZF2?

Anyone else have gone through this phase?

like image 287
Developer Avatar asked Aug 06 '12 08:08

Developer


2 Answers

Depends on the purpose of your library

Case 1, used by many modules:
Place it in your vendor folder, make sure to be PSR-0 compliant, that makes autoloading easy.

Case 2, used by only one module:
Place it in under modules/your_module/src and edit the Module.phps getAutoloaderConfig() method to have it autoloaded.

....

class Module {

....

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',      // classmap for production usage
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, // your module's files autoloading (development usage and fallback)
                'library_namespace' => __DIR__ . '/src/librarys_namespace/potential_subfolder', // your library files autoloading (development usage and fallback). eg: 'acme' => '/src/acme/library' for acme namespace
            ),
        ),
    );
}

....

Case 3, your library is 3rd party module:
Place it within the vendor folder, for references look at ZfcUser

I think your use-case would most like be case 1, your library modifies behaviour of e.g. the Zend\Mvc\Controller\AbstractActionController or additional plugins. But if a plugin is only used by one module you'll be better of placing it parallel to your Modules code as described in Case 2.

like image 193
Samuel Herzog Avatar answered Oct 06 '22 22:10

Samuel Herzog


./vendor if your code has generic purposes (i.e.: Classes like StdClass, ArrayAccess, Iterator, etc...). In short, if those Classes are required for Modules to work, they should be inside vendor.

./module In case your Plugins/Code are meant for a specific purpose (and Standalone), you may evaluate if it's a module or not (i.e.: ZF-Commons 3rd Party Modules/Plugins like ZfcUser)

like image 41
Sam Avatar answered Oct 07 '22 00:10

Sam