ZendFramework - Where to place additional classes

2019-04-03 05:52发布

I'm currently working on some kind of web application based on ZendFramework. In one of the modules there's a need of calculating item price. When buying, by providing some promotional code client can get access to one or more discounts.

I thought of making it as decorators (pattern).

Base class calculating price (method "getPrice") can be modified by one of more decorator classes. Each of them can substract some value. So, each promotion has its own class implementing some interface (e.g. with method "getPrice")

Of course example is simplified ;)

My question is, where to place this bunch of classes (and interface) in directories structure *

*I'm using default ZF's modular directory structure.

They have something in common with models, maybe helpers? But definitely nothing in common with views...

Another thing is, that they should be bound with module, so they should be placed in that module folder.

Any help, any ideas will be appreciated :)

2条回答
来,给爷笑一个
2楼-- · 2019-04-03 06:13

Sounds like you want these to go into application/modules/somemodule/models so a class like Somemodule_Model_SomePromotion would reside in application/modules/somemodule/models/SomePromotion.php.

I believe that the default resource autoloader for a modular setup will be configured to load models with classnames of hat format from that models folder. But if not, then add the following into the module (not the app) Bootstrap:

protected function _initResourceLoader()
{
    $resourceLoader = new Zend_Application_Module_Autoloader(array(
       'namespace' => 'Somemodule_',
       'basePath'  => dirname(__FILE__),
       'resourceTypes' => array(
            'models' => array(
                'namespace' => 'Model_',
                'path'      => 'models',
            ),
       ),
    ));
}

Update

Per Comment by @brady.vitrano: If the module bootstrap extends Zend_Application_Module_Bootstrap, then a resource autoloader containing this mapping comes for free. No need to include it manually, unless you wish to add other resource types.

查看更多
爷的心禁止访问
3楼-- · 2019-04-03 06:27

You should structure your own library like this:

/application
    /modules
        // Module structure
/library
    /Package
        /Subpackage
            /ClassName.php
    /Zend
        // Zend Framework Files

Where your class name is Package_Subpackage_ClassName. You should also add your Package_ prefix to the autoloader.

查看更多
登录 后发表回答