I'm a bit puzzled about Zend Framework modules.
I mean - I understand that you would usually want to have frontend and backend module... right?
But - What else would you separate out into modules?
can someone who uses Zend Framework professionally give an example of what modules they have in their application?
In the Zend MVC lingo a module is an independent part of your application. For example if you want to write a Content Management System you will probably have different modules like
Main application (user login, application bootstrap etc.)
Blog
News
Admin panel
Image gallery
Basically each of these modules could act as a separate application (although they will eventually be interconnected). The module approach also gives you an easy way for permission management (e.g. users can only pay for using certain modules).
As addition to Daffs answers we could try to figure out some guidelines for deciding between choosing a 'controller' or a 'module' to implement a certain bundle of functionality. Please help making this list comprehensive by commenting or editing:
When to use a 'controller':
when there is a considerable degree of dependency on different models of the same module
when there is a considerable degree of interaction with other controllers of the same module
when the functionality can be covered with a few methods/actions
when the functionality is clearly just a bundle of subordinated functionality of the given module
when the functionality can be easily maintained together with the rest of the code (versioning, deployment, ...)
When to use a 'module':
when there are no or almost no dependencies with the main modules controllers and models
when the bundle of functionality can be seen as a separate (sub)application
when the functionality can't be covered with a few methods/actions and thus has to be broken down into several controllers
when versioning and deployment needs independence from main module
Related
How can I manipulate other modules without editing them ? very the same thing that wordpress modules do .
They add functionality to core system without changing the core code and they work together like a charm.
I always wanted to know how to implement this in my own modular application
A long time ago I wrote the blog post "Use 3rd party modules in Zend Framework 2" specifically about extending Zend Framework 2 modules. The answer from Bez is technically correct, it could be a bit more specific about the framework.
Read the full post at https://juriansluiman.nl/article/117/use-3rd-party-modules-in-zend-framework-2, but it gives you a clue about:
Changing a route from a module (say, you want to have the url /account/login instead of /user/login)
Overriding a view script, so you can completely modify the page's rendering
Changing a form object, so you could add new form fields or mark some required field as not required anymore.
This is a long topic, but here is a short gist.
Extensibility in Zend Framework 2 heavily relies on the premise that components can be interchanged, added, and/or substituted.
Read up on SOLID principles: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
Modules typically consists of objects working together as a well-oiled machinery, designed to accomplish one thing or a bunch of related things, whatever that may be. These objects are called services, and managed by the service locator/service manager.
A big part of making your module truly extensible is to expect your developers to extend a class or implement a certain interface, which the developer register as services. You should provide a mode of definition wherein the developers can specify which things he wants to substitute, and/or add their own services to -- and this is where the application configuration comes in.
Given the application configuration, you should construct your machinery a.k.a. module services according to options the developer has specified i.e., use the developer defined Foo\Bar\UserService service as the YourModule\UserServiceInterface within your module, etc. (This is usually delegated to service factories, which has the opportunity to read the application configuration, and constructs the appropriate object given a particular set of configuration values.)
EDIT:
To add, a lot can be accomplished by leveraging Zend's Zend\EventManager component. This allows you to give developers the freedom to hook and listen to certain operations of your module and act accordingly (See: http://en.wikipedia.org/wiki/Observer_pattern)
Coming from ZendFramework 1 to ZendFramework 2 I'm a bit confused as to the functionality of the modules. In ZF1 a module was a sub-application, so you'd have a 'default' module and maybe an 'admin' module that would handle the administration section of the system and there'd be models, views, and controllers underneath each of those modules.
It seems that in ZF2 a module is more fine-grained? In the tutorial project they created an "Album" module which seems like it is only configured for a single controller. What if you wanted to maintain users and they each had a bunch of albums? Would you create another module for 'Users' with it's own controllers/views/models, or would you create a 'UserController' inside of the 'Album' module?
What if you wanted to provide an api endpoint into those controllers? Is that yet more modules, or do you provide a route in the album controller pointing to '/api/album' which specifies a rest style controller?
Zend 2 takes a modular approach to everything. It is really up to you how fine grained you want your modules to be. If album and user are decoupled and could be reusable then it makes sense to have them in separate modules. Since an album may not ALWAYS have users and vice versa. However if you write the code in such a way that user and album are tightly coupled they might as well be in the same module. zf2 is very flexible and this is really a design decision you need to make
With regard to your API question I would say you would have the Rest controller and the controller in the same modulle and use the module config routing as you say
I have existing project written in Zend Framework 1, it is long term project, constantly developed and without possibility of migrating to ZF2. It would be really neat to use Symfony2 Web Profiler bundle in it.
Currently, in development, I am using zfdebug (https://packagist.org/packages/spekkionu/zfdebug) which is great, but bundle from Symfony2 has so much more to offer...
I managed to incorporate Composer into my application (in Bootstrap), so loading something with it should be no problem. Also I found package on Packgaist (https://packagist.org/packages/symfony/web-profiler-bundle) but to be honest - I don't know if it is even usable without Symfony2.
Thanks for any tips.
No, this is not possible. If you take a look at the requirements on packagist you see it requires symfony/http-kernel, symfony/routing and symfony/twig-bridge to work. That's because the way the WebProfilerBundle works:
It registers himself at the most common events, the events happening in the HttpKernel and Routing component. If he cannot register to these events, he will not be able to give you timer information.
Moreover, it uses another event to inject imself in your page, meaning that if you don't have that event, you will never see the bar.
And the bundle is using Symfony conventions and techniques, meaning that it cannot run on ZF conventions and techniques. This is why it is called a Bundle instead of a Component, components are stand alone, bundles aren't.
Do you have it as a separate Module, or just one separate Controller, or multiple Controllers without any logical separation from Frontend (except for Auth ofcourse), or something else?
Assuming that backend is reasonably complicated, i.e. something more than review/confirm/delete comments for your BLOG.
Definatly belongs in a new module then it is easy to create a separate layout in my opinion.
In a recent project a colleague created a separate admin controller in each module with the layout defined in an admin module. This way he could drop in/remove modules from the project with the main admin module autodetecting which modules are installed and creating menus based on the admin controller found in each pluggable module.
I have explained that poorly, but it's a good system.
I would create a separate module. Otherwise you might run into context issues because the ArticleController in the frontend should have other/limited functionalities then the administrative ArticleController and you end up implementing mixed controller classes (bad idea!) or prefix the administrative class.
So having multiple modules makes it easier to separate different concerns.
(Plus it will make it easier for you to implement an ACL and handle the corresponding resource/role definitions.)
I'm in the middle of converting an existing app built on top of zend framework to work as a plugin within wordpress as opposed to the standalone application it currently is.
I've never really used zend so I've had to learn about it in order to know where to begin. I must say that at first I didn't think much of zend, but it's funny because the more I understand how it works the more I keep questioning why I'd want to remove dependency when it's a clearly well thought out framework. Then I'm reminded that it's because of wordpress.
Now I already know there are WP plugins to make zend play nice with WP. In fact I'm aleady using a zend framework plugin just to get the app functional within the WP admin area which is allowing me to review code, modify code, refresh the browser, review changes, debug code, again and again.
Anyway, I really don't have a specific question but instead I'm looking for advice from any zend masters out there to offer advice on how to best go about a task like this one.... so any comments, advice, examples or suggestions would be super.
One area I'm a little stuck on is converting parts of zend->db calls to work as wpdb calls instead... specifically the zend->db->select.... not sure what to do with that one.
Also on how to handle all the URL routing with automatic calls to "whatverAction" within thier respective controllers files.
Any help would be great! Thanks
You're probably facing an uphill battle trying to get some of the more major components of ZF to work in harmony with Wordpress. It sounds like you've got a full MVC app that you're trying to integrate into a second app that has very different architecture.
You probably want to think about which components handle which responsibilities. Wordpress has it's own routing and controller system that revolves around posts, pages and 'The Loop'. This is entirely different from Zend's Action Controllers and routing system.
It's possible you could write a WP hook to evaluate every incoming request and decide if it should be handled by WP or a ZF controller. However, it is doubtful you would be able to replace WP's routing system outright with ZF's or vice versa.
Same idea, where Zend_Db is concerned. There's nothing stopping you from using Zend_Db to access Wordpress's database, but trying to somehow convert or adapt Zend_db calls into wpdb calls sounds painful. If you have a large model layer, you probably want to hang on to it, and find a way to translate data from those models into the posts/pages conventions that Wordpress uses.
Personally, I would use ZF to build a robust business layer that can be queried through an object model via a Wordpress plugin, and then rely on Wordpress to do the routing and handle the views.
Zend_DB_Select is simple SQL query (but created using objects) that can be used like any other query. Just turn it into string. Ex.:
mysql_query((string)$zendDbSelectObject);