2

我已经使用Auth创建了Acl模块,这两个运行良好。现在我想以不同的配置多次使用这些模块。

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

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

并且两者都有不同的session变量和不同的database tables.
我有Auth如下配置文件(module.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',
        )
    ),

    ....
    ....
);

我想对Adminsection 和Usersection 使用相同的模块,但是具有不同的配置设置,例如不同的database tablessession variables.

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

4

1 回答 1

0

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

我希望你能得到基本的想法。如果没有,请告诉我。

配置/自动加载/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'];
    }
}

模块/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');
                }
        ),
    ),
];

模块/MyModuleA/src/AuthClient.php

namespace MyModuleA;

class AuthClient implements \Zend\ServiceManager\ServiceLocatorAwareInterface
{
    public function doSomethingWithAuth()
    {
        if ($this->getServiceLocator()->get('MyModuleA\Auth')->isAuthorized()) {
            // blah...
        }
    }
}
于 2013-11-13T19:36:11.523 回答