zf2 application folder repetition in 'Modules/

2019-07-30 02:05发布

问题:

Why have zf2 Application folder inside Modules/ and Modules/Application/src? I mean... why not the structure Modules/Application/src/controller, etc..?

回答1:

You can find details of the new zf2 structure http://akrabat.com/zend-framework-2/modules-in-zf2/

As we're using PHP 5.3, we expect that the classes within the src folder will be namespaced. By default, a module's main name is assumed to be its main namespace, Simple in this case. In order to make autoloading simpler and to follow the PSR-0 convention, we create the Simple namespace folder within src. This means that the namespace for the class SimpleController is Simple\Controller within the file src/Simple/Controller/SimpleController.php.

This is obviously one of the more simple modules and it is likely that the src/Simple folder would have other folders such as Model, Form and such like. Also, the view folder would probably have more sub-folders for other controllers.



回答2:

Modules are intended to be single-purpose, reusable containers of code. As such, they will contain everything they need to accomplish that task -- which may (or may not!) include controllers, models, public assets, configuration, view templates, and more.

In order to keep the various pieces semantically grouped, each has its own subfolder in the module. Source PHP code goes in "src", and the code in that directory should follow PSR-0 naming conventions - and thus you'll see the module namespace repeated there. Configuration goes in "config", view templates in "view", public assets in "public", and so on.

That said, you're free to organize your modules however you want, so long as its Module class is discoverable, and it can indicate how to autoload classes and provide configuration to the MVC. The structure you see in the ZendSkeletonApplication and ZendSkeletonModule, however, is recommended as it's the result of a lot of brainstorming within the community, and provides clean semantics for each type of file provided.