Reputation: 13275
Over the last few weeks, I saw a few Zend Framework 2 projects. While comparing them, I noticed they basically all follow the same structure:
A module named application
handles all actions for the site – the "Frontend logic" for the different parts of the application (like news posts, user profile, email settings etc.).
For every part of the application, there is at least one module, but that only contains the "Backend logic" (adding news, deleting users etc.).
I think they all build on top of the Zend Framework 2 Skeleton Application.
Is this a best practice or are there any other common or recommended patterns on how to structure an application? Why can't I write a news posts module that contains backend & frontend parts?
Also, how modular should I write my modules – I mean it's possible to spread simple functions into several modules, but it may not be always wise to do so. Are there any indicators?
Upvotes: 2
Views: 549
Reputation: 32650
Is this a best practice or are there any other common or recommended patterns on how to structure an application?
The answer is no, there is no predifined structure for Zend Framework 2 applications, contrary to ZF1. You are free to define the structure that fits your needs. This is due to the Zend\Loader\StandardAutoloader which builds the path to the class referenced by prepending the base directory path for that namespace to the class name, and then include that Class. So the structure of your application folder directory can be organized as you want.
This standard structure which is used by skeleton application is just a recommandation by the ZF2 Team. And your are not obliged to follow it if you believe that your needs are best adapted by a different structure.
zf2application
├── config
│ ├── application.config.php
│ └── autoload
│ ├── global.php
│ └── local.php.dist
├── data
│ └── cache
├── init_autoloader.php
├── module
│ └── Application
├── public
│ ├── css
│ ├── images
│ ├── index.php
│ └── js
└── vendor
├── ZF2
└──init_autoloader.php
The module's structure defined and recommanded by the Zend Team respects the PSR0 standard. You can add modules with a different structure that the one recommanded but you have to respect some rules in addition to the PSR0-standard, among other things the module.php
file in the root of the module.
You could read more in the Rob Allen's post : Thoughts on module directory structure, which explains how you can change the modules directory structure whilst respecting the recommanded standards.
Also, how modular should I write my modules?
A module is a component which regroups one or many functionalities (frontend/backend/design) of the application. The main advantage of using modules is that they separate your code and thay can be re-used in many other applications. Having this idea on mind, you'll be able to know when you should create a module and when you shouldn't.
You could also refer to this excellent post on how to write better Zend Framework 2 modules.
Upvotes: 3
Reputation: 33148
This is just down to personal preference of the developer. The skeleton application starts with just one module called 'Application' which is why you might see that a lot. It is not required - I normally rename it to something more suitable.
You can organise modules however you wish. You could certainly create a news module with both frontend and backend functionality - structure them in whatever way makes sense for your application.
When considering how 'modular' to make things, think about whether you might want to reuse that functionality in another project in the future. If yes, then making it into a module of its own might make sense.
Upvotes: 0