zend-framework - Configuration issues of getServiceConfig function in zend framework2 and 3
phpcn_u1582
phpcn_u1582 2017-05-16 16:43:14
0
1
658
//Module.php中的一段代码(项目是zend framework2官网上的简单例子)
 public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' =>  function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
//zend framework3中的样子
 public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\AlbumTable::class => function($container) {
                    $tableGateway = $container->get(Model\AlbumTableGateway::class);
                    return new Model\AlbumTable($tableGateway);
                },
                Model\AlbumTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }
  1. $smWhat type is it?

  2. Model\AlbumTableGateway::classHow do you understand this? The class AlbumTableGateway does not appear in the entire project, only the class AlbumTable

phpcn_u1582
phpcn_u1582

reply all(1)
洪涛

自己找了一个相似的问题,原文在下面,翻译在更下面。

source: AlbumTableGateway in Zend Framework 2 User Guide

The best way to think of this is that ServiceManager's get() method takes a key value, not a class name. The key value needs to map to something that will result in a class instance being returned.

If the key is within the invokables section, then the ServiceManager will try to instantiate the string that the key points to on the assumption that it's a classname:

'invokables' => array(
    'some_name' => 'My\Mapper\SomeClassName',
),

If the key is within the factories section, then the ServiceManager will execute the callback that the key points to and expect an object instance to be returned:

'factories' => array(
    'some_name' => function($sm) { return new \My\Mapper\SomeClassName(); },
),

In general, you use a factory when you need to do something more than just instantiate a class - usually you need to set up the class with another dependency. If you just need to instantiate a class, use an invokable.


翻译:
最好这样想:ServiceManagerget()方法接受一个而不是一个类名,这个会去匹配invokablesfactories中的元素并返回一个创建的对象。ServiceManagerget()方法接受一个而不是一个类名,这个会去匹配invokablesfactories中的元素并返回一个创建的对象。
如果这个是处于invocables如果这个是处于invocables的区域, 它就会实例化匹配到的那个类。

'invokables' => array(
    'some_name' => 'My\Mapper\SomeClassName',
),

如果这个处于工厂里,就会通过指向的callback函数实例化一个对象返回。

'factories' => array(
    'some_name' => function($sm) { return new \My\Mapper\SomeClassName(); },
),

(如果都没匹配到就报错了)
一般来说,只有当你不仅仅是实例化一个已存在的类,而是要去构建一个有其他依赖的类的时候才会使用factories,否则的话就用invokables就好了

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template