Zend Framework 2 getControllerConfig vs getConfig

时间:2014-05-01 20:27:14

标签: php zend-framework2

我在这里听这个演讲:https://www.youtube.com/watch?v=Vp7y65rnN98

在44:00 Module.php::getConfig() vs Module.php::getControllerConfig()

查看示例模块,如ZFCampuses zf-apigility-admin module ...

我看到定义控制器工厂的config/module.config.phpModule.php::getControllerConfig()

ZF-apigility /配置/ module.config.php

'controllers' => array(
    'aliases' => array(
        'ZF\Apigility\Admin\Controller\HttpBasicAuthentication' => 'ZF\Apigility\Admin\Controller\Authentication',
        'ZF\Apigility\Admin\Controller\HttpDigestAuthentication' => 'ZF\Apigility\Admin\Controller\Authentication',
        'ZF\Apigility\Admin\Controller\OAuth2Authentication' => 'ZF\Apigility\Admin\Controller\Authentication',
    ),
    'invokables' => array(
        'ZF\Apigility\Admin\Controller\App' => 'ZF\Apigility\Admin\Controller\AppController',
        'ZF\Apigility\Admin\Controller\CacheEnabled' => 'ZF\Apigility\Admin\Controller\CacheEnabledController',
        'ZF\Apigility\Admin\Controller\FsPermissions' => 'ZF\Apigility\Admin\Controller\FsPermissionsController',
    ),
    'factories' => array(
        'ZF\Apigility\Admin\Controller\Documentation' => 'ZF\Apigility\Admin\Controller\DocumentationControllerFactory',
        'ZF\Apigility\Admin\Controller\Filters' => 'ZF\Apigility\Admin\Controller\FiltersControllerFactory',
        'ZF\Apigility\Admin\Controller\Hydrators' => 'ZF\Apigility\Admin\Controller\HydratorsControllerFactory',
        'ZF\Apigility\Admin\Controller\Validators' => 'ZF\Apigility\Admin\Controller\ValidatorsControllerFactory',
        'ZF\Apigility\Admin\Controller\InputFilter' => 'ZF\Apigility\Admin\Controller\InputFilterControllerFactory',
    ),
),

ZF-apigility / Module.php

 public function getControllerConfig()
    {
        return array('factories' => array(
            'ZF\Apigility\Admin\Controller\Authentication' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $model    = $services->get('ZF\Apigility\Admin\Model\AuthenticationModel');
                return new Controller\AuthenticationController($model);
            },
            'ZF\Apigility\Admin\Controller\Authorization' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $factory  = $services->get('ZF\Apigility\Admin\Model\AuthorizationModelFactory');
                return new Controller\AuthorizationController($factory);
            },
            'ZF\Apigility\Admin\Controller\Config' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                return new Controller\ConfigController($services->get('ZF\Configuration\ConfigResource'));
            },
            'ZF\Apigility\Admin\Controller\ModuleConfig' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                return new Controller\ModuleConfigController($services->get('ZF\Configuration\ConfigResourceFactory'));
            },
            'ZF\Apigility\Admin\Controller\ModuleCreation' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $model    = $services->get('ZF\Apigility\Admin\Model\ModuleModel');
                return new Controller\ModuleCreationController($model);
            },
            'ZF\Apigility\Admin\Controller\Source' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $model    = $services->get('ZF\Apigility\Admin\Model\ModuleModel');
                return new Controller\SourceController($model);
            },
            'ZF\Apigility\Admin\Controller\Versioning' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $factory  = $services->get('ZF\Apigility\Admin\Model\VersioningModelFactory');
                return new Controller\VersioningController($factory);
            },
        ));
    }

我的问题是,为什么这种情况会在这里发生翻倍?关于使用Module.php::getControllerConfig()Module.config.php仅列出工厂的错误,我错过了什么?

编辑:我想我在https://stackoverflow.com/a/22374072/389976

找到了我的问题的答案

1 个答案:

答案 0 :(得分:4)

您将看到Module.php中定义的所有工厂都使用闭包(AKA匿名函数)。惯例是所有配置都进入module.config.php,闭包除外。

ZF具有内置功能(默认情况下已禁用)以缓存合并的配置文件,但如果配置文件包含闭包,则无法执行此操作,因为这些不能被序列化,因此无法缓存。通过将它们分开,您可以启用配置缓存,从而提高性能。

编辑:也许我们在这里交叉目的。我上面的答案有希望解释为什么Module.php的闭包不应该移动到模块配置文件。您当然可以为定义的每个闭包创建工厂,然后在模块配置中列出它们。有些人会考虑这种最佳做法(我个人很少这样做)。在这一点上,它是易于开发和性能之间的权衡。创建一个四行闭包比创建一个全新的类更方便。