简体   繁体   中英

ZF2 create common lib for database access across all modules

I'm starting with ZF2 and I have an application with multiple modules that are being treated as different 'sites'. I want to have a common lib that handles all data access.

Ideally, I would create something like "common" in the root directory which holds entities, mappers and services.

I've done this, but I can't seem to get it to autoload the new namespace which is outside the context of the module.

I've tried using:

    Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => true,
            'namespaces' => array(
                'Common' => dirname(__DIR__) . '/site/common',
            )
        )
    ));

Then I created the directory structure:

Common/Entity/User/User.php
Common/Mapper/User/User.php

But when I try to use these it doesn't find the files.

Any idea if there's a recommended or best practice for doing this - or how do accomplish what I'm doing?

Thanks!

The recommendation for best practice is to keep everything inside a module. Zf2 is modular application and services like data access should be part of a module as well. I would advise you too create this "common" structure as a module (below is a link of the ZF2 skeleton module).

https://github.com/zendframework/ZendSkeletonModule

In the link bellow you will find some useful information about how to prepare your module and prepare the service manager from it, to provide those mappers and entities.

http://framework.zend.com/manual/2.0/en/modules/zend.module-manager.intro.html

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM