多次使用单个模块


Use of single module multiple times

我使用 zend-framework2 创建了AuthAcl模块,这两个都运行良好。现在我想以不同的配置多次使用这些模块。

基本上,我的项目中有两个部分 -

  1. 用户部分
  2. 管理部分

两者都有不同的session变量和不同的database tables.
我有如下Auth配置文件(模块.config.php) -

return array(
    'auth' => array(
        'db' => array(
            'table' => 'user',
            'identity' => 'email',
            'credential' => 'password',
            'credential_treatment' => array(
                'class' => ''My'Common',
                'method' => 'encrypt'
            ),
            'status' => array(
                'is_enabled = true',
            ),
        ),
        'view' => array(
            'label' => array(
                'identity' => 'Email',
            ),
        ),
        'route' => array(
            'login' => 'home',
            'logout' => 'home',
        ),
        'whitelist' => array(
            'home',
        )
    ),
    ....
    ....
);

我想Admin部分和User部分使用相同的模块,但配置设置不同,例如不同的database tablessession variables.

是否可以这样做,或者我必须为不同的部分创建不同的模块?
如果您需要更多详细信息,请告诉我。

这是适用于您的用例的简单方法。我没有测试它,所以期待一些错别字:)

我希望你明白基本的想法。如果没有,请告诉我。

config/autoload/auth.global.php

return [
    'service_manager' => [
        'factories' => [
            'YourAuthNamespace'Config' => 'YourAuthNamespace'Service'ConfigServiceFactory',
            'YourAuthNamespace'AbstractAuthFactoryFactory' => 'YourAuthNamespace'Service'AbstractAuthFactoryFactory',
        ],
        'abstract_factories' => [
            'YourAuthNamespace'AbstractAuthFactoryFactory'
        ]
    ]
];

src/YourAuth/Service/AbstractAuthFactoryFactory.php

namespace YourAuth'Service;
class AbstractAuthFactoryFactory implements 'Zend'ServiceManager'AbstractFactoryInterface
{
    public function canCreateServiceWithName('Zend'ServiceManager'ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $key = $this->getConfigKeyFromServiceName($name);
        $config = $this->getConfig($serviceLocator);
        return array_key_exists($key, $config);
    }
    private function getConfig('Zend'ServiceManager'ServiceLocatorInterface $serviceLocator)
    {
        return $serviceLocator->get('YourAuthNamespace'Config');
    }
    public function createServiceWithName('Zend'ServiceManager'ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $key = $this->getConfigKeyFromServiceName($name);
        $config = $this->getConfig($serviceLocator);
        return new YourAuthClass($config[$key]);
    }
    private function getConfigKeyFromServiceName($name)
    {
        return preg_replace('#^YourAuthNamespace'Auth'#i', '', $name);
    }
}

src/YourAuth/Service/ConfigServiceFactory.php

namespace YourAuth'Service;
use Zend'ServiceManager'FactoryInterface;
class ConfigServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config');
        return $config['yourauth'];
    }
}

module/MyModuleA/config/module.config.php

return [
    'yourauth' => [
        'ModuleA' => [ // 'ModuleA' is also the key used by the abstract factory
            'db' => [
                'table' => 'module_a_auth_table',
                // ...
            ],
            'session' => [
                'namespace' => 'module_a_session_namespace'
                // ...
            ],
        ]
    ],
    'service_manager' => array(
        'factories' => array(
            'MyModuleA'Auth' => function ($sm) {
                    return $sm->get('YourAuthNamespace'Auth'ModuleA');
                }
        ),
    ),
];

module/MyModuleA/src/AuthClient.php

namespace MyModuleA;
class AuthClient implements 'Zend'ServiceManager'ServiceLocatorAwareInterface
{
    public function doSomethingWithAuth()
    {
        if ($this->getServiceLocator()->get('MyModuleA'Auth')->isAuthorized()) {
            // blah...
        }
    }
}