I devlopp web applications with Zend Framework.
For now, I have a huge library, wich contains each an every things, used by a couple of web applications. I am thinking of reorganising it, using the concept of "Module".
But I am not sure about the difference between Module and Package.
What I understand is :
a Module contains a part of a web application (pages, models...)
a Package is a group of class in the library
Am I seeing it right ? And how to know where should go my classes (model of a module, or library) ?
Because for example, I have some classes to do the translations. I have "model" classes, to represent a language, a text and its traductions... And I have kind of an "API" class that is just here to translate a string into a language. I would say that I need a module for the model classes and the web interface to edit the traductions, and the API class would go in the library ? Is that right ? Isn't that weird to have 2 kind of classes, one for the module and one for the library.
I guess that's an open question about API, librairie and application architecture.
Modules, in the ZF sense, group concrete, often standalone, application parts:
Modules allow a developer to group a set of related controllers into a logically organized group. The structure under the modules directory would resemble the structure under the application directory. […] The directory structure for modules should mimic that of the application/ directory in the recommended project structure
A package on the other hand is a set of classes in a code library that conceptually belong together. For instance, ActionHelpers and ControllerPlugins conceptually belong to the Zend_Controller package. All available Validator classes belong to the package Zend_Validate.
When using PHPDocumentor you can annotate your code to belong to packages. If you look at ZF's API Docs, you will see this grouping in effect. Try to find the Zend_Validate_Alpha class.
See
http://framework.zend.com/manual/en/project-structure.project.html
http://framework.zend.com/manual/en/project-structure.filesystem.html
http://framework.zend.com/apidoc/core/
http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.package.pkg.html
packages are namespace areas so that symbols with similar names do not clash with one another. For example, the symbol &main::first is different from symbol &List::Util::first . Packages are name prefixes to sysbols.
A module is a file of code, or a tree of bytecodes. A module could be precompiled (.pmc), uncompiled (.pm) on disk; or pre-loaded in memory as one unit -- assuming no autosplit.
In summary: packages are about namespaces, and modules are about files. They are different things, like apples and boxes -- until the day when you start placing one kind of apple into one kind of box, and people start thinking that apples and boxes are related. And they are! but still, one in fruit and the other is a type of container.
Simple in my view:
Modules are like smarty (a library with a targeted goal templating).
Packages are 2 or more modules that work with each other to offer a more complete solution.
Again my simple point of view.
Related
I am currently planning on a pretty big project that would be split in 4 modules.
Core module (default) - manage ACL, Auth and relations between other modules
Sections - Will separate the differents section of the application and will act as a timeline / progress bar (Locked and unlocked sections based on progress)
CMS - Will provide global content and section specific content
Exams
For those module I will have to develop a back end to edit content/right and so on. What would be the best way to create these?
For now I see two options (there might be more) :
Build the frontend with modules and controller and create a controller within the module with an admin_ prefix
Create an other module for each module, like : core, core_admin, sections, sections_admin, etc.
This depends on how loosely coupled you want to make your modules.
If the modules themselves are never going to move between projects, an admin module might be the simplest solution. The downside to this is that the admin module will be tightly coupled with the other modules. This is the most logical approach from your description.
If you want to keep them loosely coupled, creating separate modules as you mentioned is probably your best route.
Alternatively, if there is not a whole lot to manage, you could always simply create an AdminController in each module and have individual actions for all the various admin tasks. This would get messy as the project grows though.
With any of the loosely coupled choices, you may want to look at using the Zend Regex Router so you can provide a more consistent route to your admin interface.
As our company starts using Zend Framework as the base framework for most of our projects, we want to share some common elements across all our projects. I talk about things like:
An implementation of a model (based on doctrine2)
RBAC for the model, including user, group, role models
A xml-based templating engine for ajax backend interfaces
(you name it) ...
Basically, all things to put "zend on rails" and get going. What is the best way to package these components? I see two possibilities:
As modules
We include the necessary functions as separate modules into the modules folder.
Pro:
We can set routes and execute code, which is good for many modules (imaginary example: a paypal module needs some kind of callback url. If our module can set it up on its own, no configuration from the "project developer" is needed).
We can provide real functionality (like the user administration) out of the box
We have a bootstrap to set up autoloading and doctrine etc.
Con:
Bad place? Interferes with the users project
A little harder to share between projects (git submodules instead of classpath)
In the library folder
We put it in the library folder and point the classpath to it.
Pro:
Clean solution
Sharing across projects
Con:
Bootstrap has to be explicitly called
No direct routing or actions - everything has to be proxied through the concrete project
So, how do you solve this? Where do you put your reusable, general purpose stuff in zf?
I think you should use both approaches.
When developing "library-like" code, as in kind of "infrastructure" classes and other things that are reusable (like ZF's own components, Doctrine 2's components etc.), you can put them into the library directory. (or its own entirely separate project)
When developing actual ZF modules (like an auth module for example), then format the code around the ZF module structure.
I think by using this kind of approach you get all the benfits you listed, and pretty much none of the cons :)
As one additional idea, if you develop your architecture parts as "services", you could even keep them running as their own web service endpoints.
I'm starting to familiarize myself with using the module-based architecture for zend framework projects. My real reason behind being interested in the module architecture is to be able to take a module from one project and just drop it into another project. Maybe I'm not getting it right..
But what I'm noticing right off the bat is that controllers within each module cannot have the same name as any other controller in the main application (or in any other module, though I haven't tested this). This leads me to think that modules are not really independent self-contained units, so I wonder how this affects their ease of distribution from one project to another.
The other issue is what if I were to take a module and drop it into another project. Do I have to update the .zfproject.xml manually? and wouldn't that be a bit too cumbersome to be done manually?
Maybe I'm not clear on how modules should be used in zend, so I'd like to know when you decide it's best to use them, and when do you decide not to use them, or do you use them all the time, or do you never use them?
I always used module based architecture so far in my projects, because I like to separate concepts. For example I have always an ADMIN module whose classes and controllers dont mix with the rest of the application. Using modules you can reuse modules for other applications, for example if you create a BLOG module.
The names of your controllers will be something like Admin_IndexController for the admin module even if the file is named IndexController.php.
Another concept that is nice and help you reuse resources is the plugins. Use them for authentication or to check validity of the requests.
You need to setup namespaces for your modules so that they are easily moved into a new project without renaming.
If you are using Zend Tool then you will have to edit the zfproject.xml. I haven't spent a lot of time using this so I'm not sure if there is another way without manually editing.
Besides the obvious 'admin' and 'blog' use cases for a module structure in Zend Framework are there any reasons or guidelines to use Modules? I feel drawn to use modules, but I'm not sure how I would split my web app up into modules.
Can you suggestion some instances where using modules would be beneficial?
Modules are good when you want to reuse them across multiple applications, or when a piece of the application is large enough that it warrants separation into its own chunk. Rather than having a bunch of unrelated controllers in one directory, the module allows you to split up the MVCs into related chunks.
We use modules all the time; our clients frequently pick and choose between the available modules to package an application. We also introduced module dependencies, so that modules may rely on each other.
As a general rule, when our applications have a high level subfolder, unless they have only a couple pages, they are modules. For example, About would only be a controller and a view within the application, but forum might be its own module.
I have way too many modules in my application. Currently my modules are namespaced, but what I'd like to do is have a directory structure so I can get rid of this redundant and annoying namespacing.
For instance, for modules named "xModule1, xModule2, xModule3", I'd like to have a directory structure like this:
-x
-module1
-actions and templates
-module2
-actions and templates
-module3
- actions and templates
Surely the developers at symfony know that people would like to use their framework to develop large applications. So how is module organization like this done?
I've done a lot of work in Java/Spring, and because source is component scanned, you can arrange your controllers and jsp files in nicely organized hierarchies. Is this somehow possible with Symfony?
No, this is not possible with Symfony. The structure of your modules and their actions and templates is expected in a fixed file system layout and I haven't heard anything about that changing.
I've run into the same problem you're facing where a very large site ended up with 30+ modules in a single application. At first it seemed cumbersome but after dealing with it for a while I found that the single location to search for a specific module was in fact beneficial instead of having to guess through sub-structures until I got what I was after. Seeing that structure grow and grow also pushes me to respect adding new modules only when it's absolutely necessary, folding new functionality into existing modules and refactoring existing modules to work with new enhancements whenever possible.
Symfony does have auto-loading features that will work for your library folders however, allowing you to have lib/one/two/three/Object.class.php or any other structure you see fit.
If you have so many modules, you could consider to move some functionality into plugins (i.e. create your own plugins).
The benefit is that you can use this functionality also in other projects.
Or you can group your modules into applications. You can have as many applications as you want, not only backend and frontend.
I've wondered about the same thing, especially as many configuration files need to be set either at application level or individual module level. It could useful to be able to cascade configurations to a set of modules.
As mentioned above, it seems the available solutions are:
deal with lots of modules
create separate applications (which will create some wieldy duplication)
refactor your modules to be as efficient as practical (i.e. multiple controllers & views per module)