小编Pon*_*juh的帖子

为什么Servicemanager注入了invokable而不是service

我正在使用ZF2 serviceManager,我正在试图找出为什么serviceManager不会在实现ServiceLocatorAwareInterface的类中注入sm.

我的主要问题是我做得对,还是"服务"键不是用于实现ServiceLocatorAwareInterface的服务,而是用于不需要注入的服务?

在Module.php中

public function getServiceConfig() {
    return array(
        'invokables' => array(
            'myService1' => 'MyModule\Service\Service'
        ),
        'services' => array(
            'myService2' => new MyModule\Service\Service(),
        ),
    );
}
Run Code Online (Sandbox Code Playgroud)

在MyModule\Service\Service.php中

namespace MyModule\Service;

use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;

class Service implements ServiceManagerAwareInterface
{
    /**
     * @var ServiceManager
     */
    protected $serviceManager = NULL;

    /**
     * Retrieve service manager instance
     *
     * @return ServiceManager
     */
    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    /**
     * Set service manager instance
     *
     * @param ServiceManager $serviceManager
     */
    public function …
Run Code Online (Sandbox Code Playgroud)

php zend-framework2

4
推荐指数
1
解决办法
2561
查看次数

标签 统计

php ×1

zend-framework2 ×1