invokablesはsinmpleオブジェクトをインスタンス化するために使用する必要がありますが、コンストラクタには他の依存関係などは必要ありません。
オブジェクトのインスタンス化の後ろにもう少し複雑なロジックがある場合は、ファクトリを使用する必要があります。コードを工場に移すことで、オブジェクトが必要なときにコードを複製する手間を省くことができます。
工場の例:あなたが戻って単純なクラスを使用する場合は
'factories' => array(
'Application\Acl' => 'Application\Service\AclFactory',
AclFactory.php
namespace Application\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Permissions\Acl\Resource\GenericResource;
use Zend\Permissions\Acl\Role\GenericRole;
class AclFactory implements FactoryInterface
{
/**
* Create a new ACL Instance
*
* @param ServiceLocatorInterface $serviceLocator
* @return Demande
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$acl = new \Zend\Permissions\Acl\Acl();
/**
* Here you can setup Resources, Roles or some other stuff.
* If it's complex you can make a factory like this to keep
* the code out of the service config, and it will save you
* having to duplicate the code when ever you need your ACL
*/
return $acl;
}
}
/何がだとして、あなたはただ、呼び出し可能なを使用することはできませんバックオブジェクトオブジェクトを戻すために必要なボイラープレートコード。
'invokables' => array(
'MyClass' => 'Application\Model\MyClass',
コントローラとの別の例、:
あなたがいないrequried依存関係を持つ、シンプルなコントローラを持っている場合は、呼び出し可能な使用:
'invokables' => array(
'index' => 'Mis\Controller\IndexController',
しかし、時にはあなたに余分な依存関係を追加したいがコントローラをインスタンシエートするとき
'factories' => array(
/**
* This could also be added as a Factory as in the example above to
* move this code out of the config file..
*/
//'users' => 'Application\Service\UsersControllerFactory',
'users' => function($sm) {
$controller = new \Application\Controller\UsersController();
$controller->setMapper($sm->getServiceLocator()->get('UserMapper'));
$controller->setForm($sm->getServiceLocator()->get('UserForm'));
return $controller;
},
私が工場を持っている場合、Fa ctoryInterface? Factoryクラスのコンストラクタ '__invoke'を作成すると、この悪い形式ですか? – Erik