如何在zend框架中创建一个工厂2? [英] how to create a factory in zend framework 2?
问题描述
在我的 Module.php
我有fallowing方法,我想移动他们在工厂类
所以我不会混淆模块类
:
public function getControllerConfig )
{
return array(
'factories'=> array(
'account-index'=> function($ controllerManager){
$ serviceManager = $ controllerManager-> getServiceLocator();
$ accountService = $ serviceManager-> get('account-service');
return new Controller\IndexController($ accountService);
}
)
);
}
public function getServiceConfig()
{
return array(
'factories'=> array(
'account-service '=> function($ serviceManages){
return new Service\Account;
}
)
);
}
现在我有:
我应该把工厂类
放在 Factory
文件夹中吗?
我通常把我的工厂放到 ../ module / yourmodule / src / yourmodule / Factory
。
在您的 ../ module / yourmodule / config / module.config.php
service_manager like like:
'service_manager'=> array(
'factories'=> array(
'yourfactory'=>'yourmodule\Factory\yourfactory',
),
),
c>
c。 c 你必须插入FactoryInterface设置服务定位器。一旦你这样做,你应该能够以控制器,表单等通常的方式调用服务。
命名空间Application\Factory ;
使用Zend\ServiceManager\FactoryInterface;
使用Zend\ServiceManager\ServiceLocatorInterface;
class yourfactory implements FactoryInterface
{
private $ config;
private $ serviceLocator;
public function createService(ServiceLocatorInterface $ serviceLocator)
{
return $ servicelocator-> get('Your\Service');
}
之后,您可以在 yourfactory .php
。在你的控制器中你调用像 $ serviceManager-> get('yourfactory') - > yourfunction(yourarguments);
in my Module.php
i have the fallowing methods that i would like to move them in a factory class
so that i wont clutter the Module class
:
public function getControllerConfig()
{
return array(
'factories' => array(
'account-index' => function ($controllerManager) {
$serviceManager = $controllerManager->getServiceLocator();
$accountService = $serviceManager->get('account-service');
return new Controller\IndexController($accountService);
}
)
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'account-service' => function ($serviceManages) {
return new Service\Account;
}
)
);
}
right now i have:
and where shall i put this factory class
, maybe in a Factory
folder?
any ideas?
I usually put my factories into ../module/yourmodule/src/yourmodule/Factory
.
in your ../module/yourmodule/config/module.config.php
you then have to configure your service_manager like so:
'service_manager' => array(
'factories' => array(
'yourfactory' => 'yourmodule\Factory\yourfactory',
),
),
in yourfactory.php
You then have to implent the FactoryInterface and set the service locator. Once you done this you should be able to call the service the usual way for controllers, forms etc.
namespace Application\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class yourfactory implements FactoryInterface
{
private $config;
private $serviceLocator;
public function createService(ServiceLocatorInterface $serviceLocator)
{
return $servicelocator->get('Your\Service');
}
After that you can just define functions in your yourfactory.php
. In your Controller you call functions like so $serviceManager->get('yourfactory')->yourfunction(yourarguments);
这篇关于如何在zend框架中创建一个工厂2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!