Symfony 3.2 FOSMessageBundle '.../new'(即撰写新消息)无论我做什么都会抛出错误

Symfony 3.2 FOSMessageBundle '.../new' (ie compose new message) throws error no matter what I do

我没有使用 FOSUserBundle(已有自定义 user/security 代码)。我一直在关注文档 here

在对 Symfony 和 FOSMessageBundle 文件进行大量谷歌搜索和挖掘之后,我开始相信这个问题与新的 NewThreadMessageFormFactory class 有关。它只有一种方法。如果我保持原样

    public function create()
    {
        return $this->formFactory->createNamed(
          $this->formName, 
          $this->formType, 
          $this->createModelInstance());
    }

我收到此错误 Expected argument of type "string", "AppBundle\Service\NewThreadMessageFormType" given。另一方面,如果我执行以下操作,

public function create()
{
    return $this->formFactory->createNamed(
      $this->formName,
      'AppBundle\Service\NewThreadMessageFormType', 
      $this->createModelInstance());
}

我明白了 Could not load type "FOS\UserBundle\Form\Type\UsernameFormType。当然,它甚至不存在,因为甚至没有安装 FOSUserBundle。

我已经在这上面花了十几个小时了。我在这个过程中学到了很多东西,但我仍然无法使这该死的东西发挥作用...


以及我目前的配置

//config.yml
fos_message:
    db_driver: orm
    thread_class: AppBundle\Entity\Thread
    message_class: AppBundle\Entity\Message
    new_thread_form:
        type: app.new_thread_form_type
        factory: app.new_thread_form_factory

和...

  //services.yml
  fos_user.user_to_username_transformer:
      alias: app.user_to_username_transformer

  app.user_to_username_transformer:
      class: AppBundle\Form\DataTransformer\UserToUsernameTransformer
      arguments:
          type: "service"
          id: "doctrine"

  app.new_thread_form_type:
      class: AppBundle\Service\NewThreadMessageFormType
      arguments: ['@app.user_to_username_transformer']
      tags:
          - {name: form.type}

  app.new_thread_form_factory:
      class: AppBundle\Service\NewThreadMessageFormFactory
      arguments: [
          '@form.factory',
          '@fos_message.new_thread_form.type',
          '%fos_message.new_thread_form.name%',
          '%fos_message.new_thread_form.model%'
          ]

如果您不使用 FOSUSerBundle,则需要执行以下操作:

  1. 按照指南创建 UserToUsernameTransformer class Using other UserBundles
  2. 在config.yml中:

    fos_message:
        db_driver: orm
        thread_class: AppBundle\Entity\Thread
        message_class: AppBundle\Entity\Message
        new_thread_form:
            type: AppBundle\Form\Type\NewThreadMessageFormType
    
  3. 注册服务:

    app.user_to_username_transformer:
        class: AppBundle\Form\DataTransformer\UserToUsernameTransformer
        arguments: ['@doctrine']
    fos_user.user_to_username_transformer:
        alias: app.user_to_username_transformer
    app.form.type.username:
        class: AppBundle\Form\Type\UsernameFormType
        arguments: ['@app.user_to_username_transformer']
        tags:
            - { name: form.type }
    
  4. 创建一个 NewThreadMessageFormType 表单类型 class 以覆盖默认表单类型。

    class NewThreadMessageFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('recipient', UsernameFormType::class, array(
                    'label' => 'recipient',
                    'translation_domain' => 'FOSMessageBundle',
                ))
            ->add('subject', TextType::class, array(
                'label' => 'subject',
                'translation_domain' => 'FOSMessageBundle',
            ))
            ->add('body', TextareaType::class, array(
                'label' => 'body',
                'translation_domain' => 'FOSMessageBundle',
            ));
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'intention' => 'message',
            ));
        }
    
        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'new_thread_message';
        }
    }
    
  5. 创建一个 UsernameFormType 表单类型 class 来处理转换和显示用户名。

    class UsernameFormType extends AbstractType
    {
        /**
         * @var UserToUsernameTransformer
         */
        protected $usernameTransformer;
    
        /**
         * Constructor.
         *
         * @param UserToUsernameTransformer $usernameTransformer
         */
        public function __construct(UserToUsernameTransformer $usernameTransformer)
        {
            $this->usernameTransformer = $usernameTransformer;
        }
    
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->addModelTransformer($this->usernameTransformer);
        }
    
        /**
         * {@inheritdoc}
         */
        public function getParent()
        {
            return TextType::class;
        }
    
        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'toolsets_username_type';
        }
    }
    

这应该可以正常工作。