fosuserbundle 覆盖注册表,并使用配置变量
fosuserbundle override register form, and use configuration variables
我正在覆盖注册表并且工作正常:
services.yml
general_user.registration.form.type:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: general_user_registration }
我在 config.yml 中有一个变量 我想从 RegistrationFormType 访问:
# interested in options
profile.lookingfor:
- part 1
- part 2
我了解到这可以使用服务来完成,所以我定义了另一个服务:
general_user.registration.form.type.lookingfor:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%profile.lookingfor%]
tags:
- { name: form.type, alias: general_user_registration }
我希望能够使用类似以下内容从 RegistrationFormType 访问参数:
$lookingFor = $this->get('general_user.registration.form.type');
我不知道如何将这两种服务混合在一起。现在我得到错误:
Attempted to call an undefined method named "get" of class "General\GeneralBundle\Form\Type\RegistrationFormType".
Did you mean to call e.g. "getBlockPrefix", "getName" or "getParent"?
当我注释掉 get 调用时,出现另一个错误:
Cannot read index "email" from object of type "...\CoreBundle\Entity\User" because it doesn't implement \ArrayAccess
您将变量作为参数传递给服务的方式仅适用于参数(在 config.yml
或 parameters.yml
中的 parameters:
下定义的变量)。在这种情况下您不需要第二项服务。
为了在您的服务中访问它,您只需在您的服务中定义一个构造函数参数,该参数将作为数组自动注入。
protected $lookingFor;
public function __constructor($lookingFor) {
$this->lookingFor = $lookingFor;
}
如果你想为你的包使用一个配置块,它会变得有点复杂。参见:http://symfony.com/doc/master/cookbook/bundles/configuration.html
最后我使用了 Setter 注射剂。来自 http://symfony.com/doc/current/book/service_container.html#optional-dependencies-setter-injection :
如果您有 class 的可选依赖项,那么 "setter injection" 可能是更好的选择。这意味着使用方法调用而不是通过构造函数来注入依赖项。我在我的 RegistrationFormType
中添加了一个 setter 方法
protected $lookingFor;
public function setLookingfor($lookingFor) {
$this->lookingFor = $lookingFor;
}
通过setter方式注入依赖只需要添加'calls'部分:
app/config/services.yml
general_user.registration.form.type:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: general_user_registration }
calls:
- [setLookingfor, ['%profile.lookingfor%']]
我正在覆盖注册表并且工作正常:
services.yml
general_user.registration.form.type:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: general_user_registration }
我在 config.yml 中有一个变量 我想从 RegistrationFormType 访问:
# interested in options
profile.lookingfor:
- part 1
- part 2
我了解到这可以使用服务来完成,所以我定义了另一个服务:
general_user.registration.form.type.lookingfor:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%profile.lookingfor%]
tags:
- { name: form.type, alias: general_user_registration }
我希望能够使用类似以下内容从 RegistrationFormType 访问参数:
$lookingFor = $this->get('general_user.registration.form.type');
我不知道如何将这两种服务混合在一起。现在我得到错误:
Attempted to call an undefined method named "get" of class "General\GeneralBundle\Form\Type\RegistrationFormType".
Did you mean to call e.g. "getBlockPrefix", "getName" or "getParent"?
当我注释掉 get 调用时,出现另一个错误:
Cannot read index "email" from object of type "...\CoreBundle\Entity\User" because it doesn't implement \ArrayAccess
您将变量作为参数传递给服务的方式仅适用于参数(在 config.yml
或 parameters.yml
中的 parameters:
下定义的变量)。在这种情况下您不需要第二项服务。
为了在您的服务中访问它,您只需在您的服务中定义一个构造函数参数,该参数将作为数组自动注入。
protected $lookingFor;
public function __constructor($lookingFor) {
$this->lookingFor = $lookingFor;
}
如果你想为你的包使用一个配置块,它会变得有点复杂。参见:http://symfony.com/doc/master/cookbook/bundles/configuration.html
最后我使用了 Setter 注射剂。来自 http://symfony.com/doc/current/book/service_container.html#optional-dependencies-setter-injection :
如果您有 class 的可选依赖项,那么 "setter injection" 可能是更好的选择。这意味着使用方法调用而不是通过构造函数来注入依赖项。我在我的 RegistrationFormType
中添加了一个 setter 方法protected $lookingFor;
public function setLookingfor($lookingFor) {
$this->lookingFor = $lookingFor;
}
通过setter方式注入依赖只需要添加'calls'部分:
app/config/services.yml
general_user.registration.form.type:
class: General\GeneralBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: general_user_registration }
calls:
- [setLookingfor, ['%profile.lookingfor%']]