I'm in the process of learning ZF2 and planning an application.
I would like to have 1 application with several groups of modules. Each group of modules could contain up-to 20+ modules.
I won't be able to guarantee a unique name for any of the modules for the whole app, but i can guarantee their uniqueness within their module group.
Ideally route requests to module groups using a Hosting router.
e.g. http://admin.mysite.com/foo/ => 'Admin/Foo/Index/Index',
http://special.mysite.com/foo/ => 'Special/Foo/Index/Index'
Idea #1
I was hoping to use a different module_path for each module group and namespacing the module path around the module group to determine uniqueness of the module's class. But modules sent to the module_autoloader only seem to pay any attention to the first part of any strings passed.
e.g.
array('modules' => array(
'Admin\Admin','Admin\Bob','Admin\Users'
));
This gets strange behaviour I don't understand:
- All three will point to the correct module controller but they load the view for Admin\Admin (which is first in the module list) instead of their own.
- I.E. /users, loads Admin\Users\IndexController but with the view script Admin\Admin\view\admin\Index\index.phtml
I've managed to fix this with some small alterations to the templateInjecter but it seems messy.
Idea #2
Just prefix module names to their subdirectory e.g.
namespace AdminAdmin\Controller,
class IndexController {}
Thoughts
- namespaces would be preferred to class prefixes
- we're trying to automate as much of this as possible instead of using hardcoded routes for each module
- it could make sense to use multiple applications that share a module for authentication via a common cookie host/memcache and db, etc...
- route each module to a certain subdomain using the hosting type routes inside a treeRouteStack of the modules controllers etc...
Question:
- Is their a best practice for this situation or similar and why?
My interpretation of your question is that you're really asking two questions:
(1) Namespacing modules
This is possible natively with ZF2. Modules in ZF2 are basically just PHP namespaces, so a module named
Foo\Bar
is perfectly acceptable, and by default the module loader will look for it's module class inmodule/Foo/Bar
thenvendor/Foo/Bar
.For example, if you wanted to create a module Anvil and under the namespace Acme, you would create directory
module/Acme/Anvil
, and inside it create aModule.php
file:In your application's
config/application.config.php
you would addAcme\Anvil
to themodules
key:(2) Dynamically-loading modules
One approach you could take would be to modify your site's bootstrap
index.php
to modify the configuration on-the-fly based on the hostname. For example, using ZendSkeletonApplication you would set up yourapplication.config.php
file like so:To make this work we make a slight modification to
public/index.php
to intercept the configuration array loaded fromconfig/application.config.php
and reconfigure themodules
key:This approach provides an easy way to change which modules are loaded for a particular hostname, but can easily be extended to allow site-specific configuration file autoloading and site-specific module directories as well.