[英]ZF2: Groups of modules
I'm in the process of learning ZF2 and planning an application. 我正在学习ZF2并计划一个应用程序。
I would like to have 1 application with several groups of modules. 我想要一个带有几组模块的应用程序。 Each group of modules could contain up-to 20+ modules.
每组模块最多可包含20多个模块。
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. 理想情况下,使用托管路由器将请求路由到模块组。
eg http://admin.mysite.com/foo/ => 'Admin/Foo/Index/Index', 例如http://admin.mysite.com/foo/ =>'Admin / Foo / Index / Index',
http://special.mysite.com/foo/ => 'Special/Foo/Index/Index' http://special.mysite.com/foo/ =>'Special / Foo / Index / Index'
Idea #1 想法#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. 我希望为每个模块组使用不同的module_path,并对模块组周围的模块路径命名,以确定模块类的唯一性。 But modules sent to the module_autoloader only seem to pay any attention to the first part of any strings passed.
但是发送到module_autoloader的模块似乎只注意传递的所有字符串的第一部分。
eg 例如
array('modules' => array(
'Admin\Admin','Admin\Bob','Admin\Users'
));
This gets strange behaviour I don't understand: 这是我不理解的奇怪行为:
I've managed to fix this with some small alterations to the templateInjecter but it seems messy. 我已经通过对templateInjecter进行了一些小的改动来解决了这个问题,但是看起来很混乱。
Idea #2 想法2
Just prefix module names to their subdirectory eg 只需在其子目录前添加模块名称即可
namespace AdminAdmin\Controller,
class IndexController {}
Thoughts 思考
Question: 题:
My interpretation of your question is that you're really asking two questions: 我对您的问题的解释是,您实际上是在问两个问题:
This is possible natively with ZF2. 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 in module/Foo/Bar
then vendor/Foo/Bar
. ZF2中的模块基本上只是PHP命名空间,因此一个名为
Foo\\Bar
的模块是完全可以接受的,并且默认情况下,模块加载器将在module/Foo/Bar
然后在vendor/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 a Module.php
file: 例如,如果要创建模块Anvil并在名称空间Acme下创建目录
module/Acme/Anvil
,然后在其中创建Module.php
文件:
<?php
namespace Acme\Anvil;
class Module
{
/* module class code goes here */
}
In your application's config/application.config.php
you would add Acme\\Anvil
to the modules
key: 在应用程序的
config/application.config.php
您可以将Acme\\Anvil
添加到modules
键中:
return array(
'modules' => array(
'Application',
'Acme\Anvil',
),
// Remaining bits of config array are unchanged
);
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. 您可以采取的一种方法是修改站点的bootstrap
index.php
以根据主机名即时修改配置。 For example, using ZendSkeletonApplication you would set up your application.config.php
file like so: 例如,使用ZendSkeletonApplication,您可以像下面这样设置
application.config.php
文件:
<?php
return array(
'modules' => array(
'Application',
// Other modules common to all sites go here
),
'sites' => array(
'site-one.mydomain.com' => array(
'modules' => array(
'ModuleOne',
'ModuleTwo',
),
),
'site-two.mydomain.com' => array(
'modules' => array(
'ModuleThree',
),
),
),
// Remaining bits of config array are unchanged
);
To make this work we make a slight modification to public/index.php
to intercept the configuration array loaded from config/application.config.php
and reconfigure the modules
key: 为了完成这项工作,我们对
public/index.php
进行了一些修改,以拦截从config/application.config.php
加载的配置数组,并重新配置modules
键:
// Pick host out of request
$hostname = $_SERVER['HTTP_HOST'];
// Load application configuration
$applicationConfig = require 'config/application.config.php';
// Merge site-specific modules into loaded modules array
$applicationConfig['modules'] = array_merge(
$applicationConfig['modules'],
$applicationConfig['sites'][$hostname]['modules']
);
// Run the application!
Zend\Mvc\Application::init($applicationConfig)->run();
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. 这种方法提供了一种简便的方法来更改为特定主机名加载的模块,但可以轻松扩展以允许特定于站点的配置文件自动加载以及特定于站点的模块目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.