4

I would like to start working with Zend Framework 2 and need some guidance as to the architecture of the framework. I compared Akrabat's ZF2 tutorial with it's ZF1 equivalent and the main difference I noticed so far seems to be the use of modules. I like the idea with modules as being independent and re-usable pieces of code and am thinking it could help segmenting my application and make it more maintainable. For instance I could have the following URL => module mapping:

http://example.org/products => Products module
http://example.org/services => Services module
http://example.org/oauth => Oauth module
[...]

What I'm confused about, is that some of my modules will share the same underlying database resources (e.g. both Products and Services use the same table for comments) and therefore the same database models. Accordingly, I'm wondering:

Where should I put my database models when those are used by multiple modules?

1 Answer 1

3

See the following article: http://adam.lundrigan.ca/2011/09/module-dependencies-in-zf2/

You can create a third module (example: Base, Main) that is a dependency for the Products and Services modules, or you can add the Models to one of your modules and use that module as a dependency for the other one.

Module.php

/**
 * Get dependencies
 *
 * @return array
 */
public function getDependencies()
{
    return array(
        'BaseModule' => array(
            'version' => '>=0.1.0',
            'required' => true
        ),
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

that could be a way to solve this problem indeed. Do you see any issue with putting all my basic database models (i.e. setters and getters) in a module such as DatabaseModels which as you explained would be a dependency for the other modules, and extend those database models within each corresponding module and put more advanced business logic into them?
I wouldn't put there all the models, only if they are common across all modules. Put models in their own module if possible.
You can do something similar to the following ZF2 modules ZfcUser and ZfcAcl both extend a base code from ZfcBase, but there's nothing specific of them in ZfcBase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.