Zend framework2 如何使用getServiceLocator()在模型中获取适配器?
我正在学习ZF2 我可以使用Zend framework2 如何使用getServiceLocator()在模型中获取适配器?,zend-framework2,dependencies,service-locator,servicemanager,zend-servicemanager,Zend Framework2,Dependencies,Service Locator,Servicemanager,Zend Servicemanager,我正在学习ZF2 我可以使用getServiceLocator()在模型中获取适配器吗 /config/autoload/global.php return array( 'db' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=zf2tutorial;host=localhost', 'driver_options' =>
getServiceLocator()
在模型中获取适配器吗
/config/autoload/global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
);
return array(
'db' => array(
'username' => 'YOUR USERNAME HERE',
'password' => 'YOUR PASSWORD HERE',
),
);
/config/autoload/local.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
);
return array(
'db' => array(
'username' => 'YOUR USERNAME HERE',
'password' => 'YOUR PASSWORD HERE',
),
);
那么,我如何使用:
$sm = $this->getServiceLocator();
$this->dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
要在模型中获取适配器?如果您使用的是最新的Zend Framework版本,则无法在控制器类中使用
getServiceLocator
方法,因为ServiceLocatorAwareInterface
和ServiceManagerAwareInterface
都已删除
所以这一行:
$sm = $this->getServiceLocator();
在控制器类中可能无法像您预期的那样工作
您还可以在以下位置阅读有关此更改的更多信息: 已删除以下接口、特征和类:
- Zend\ServiceManager\ServiceLocatorAwareInterface
- Zend\ServiceManager\ServiceLocatorAwareTit
- Zend\ServiceManager\ServiceManagerWareInterface
同时检查(第章工厂)有关如何构建工厂的更多详细信息。如果您使用的是最新的Zend Framework版本,则无法在控制器类中使用
getServiceLocator
方法,因为ServiceLocatorAwareInterface
和ServiceManagerAwareInterface
都已删除
所以这一行:
$sm = $this->getServiceLocator();
在控制器类中可能无法像您预期的那样工作
您还可以在以下位置阅读有关此更改的更多信息: 已删除以下接口、特征和类:
- Zend\ServiceManager\ServiceLocatorAwareInterface
- Zend\ServiceManager\ServiceLocatorAwareTit
- Zend\ServiceManager\ServiceManagerWareInterface
还可以查看(第章工厂)了解有关如何构建工厂的更多详细信息。您需要在创建模型时将适配器注入模型,例如使用工厂 例如,在您的配置中:
'service_manager' => array(
'factories' => array(
'Application\Some\Model' => '\Application\Some\ModelFactory'
)
)
然后,您将创建工厂,将适配器注入到您的模型中:
<?php
namespace Application\Some;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ModelFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$adapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
return new Model($adapter);
}
}
这将呼叫工厂并带回带有适配器的完整型号
然后,您将使用相同的方法将模型注入任何需要的控制器或服务类中。如前一篇文章所述,尝试避免将服务定位器/服务管理器直接注入到对象中,而是使用它从Factory类等内部添加所需的项(适配器/映射器等)。创建模型时,您需要将适配器注入模型中,例如使用Factory 例如,在您的配置中:
'service_manager' => array(
'factories' => array(
'Application\Some\Model' => '\Application\Some\ModelFactory'
)
)
然后,您将创建工厂,将适配器注入到您的模型中:
<?php
namespace Application\Some;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ModelFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$adapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
return new Model($adapter);
}
}
这将呼叫工厂并带回带有适配器的完整型号
然后,您将使用相同的方法将模型注入任何需要的控制器或服务类中。如前一篇文章所述,请尝试避免将服务定位器/服务管理器直接注入到对象中,而是使用它从工厂类内部添加所需的项(适配器/映射器等)