简体   繁体   English

ZF2:模块组

[英]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: 这是我不理解的奇怪行为:

  • 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. 这三个都将指向正确的模块控制器,但它们将加载Admin \\ Admin的视图(在模块列表中排在第一位),而不是它们自己的视图。
  • IE /users, loads Admin\\Users\\IndexController but with the view script Admin\\Admin\\view\\admin\\Index\\index.phtml IE / users,加载Admin \\ Users \\ IndexController,但使用视图脚本Admin \\ Admin \\ view \\ admin \\ Index \\ index.phtml

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 思考

  • 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... 可能会使用多个应用程序,这些应用程序通过一个共同的cookie主机/内存缓存和db等共享一个模块进行身份验证。
  • route each module to a certain subdomain using the hosting type routes inside a treeRouteStack of the modules controllers etc... 使用模块控制器的treeRouteStack内部的托管类型路由将每个模块路由到某个子域...

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 (1)命名空间模块

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
);

(2) Dynamically-loading modules (2)动态加载模块

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM