首页 > 解决方案 > 覆盖 FOUserBundle 后如何编辑 services.yaml

问题描述

//src\窗体;


    namespace src\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;

    class RegistrationFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
            ->add('name',TextType::class,array(
                'label' => 'Nom',
                'required' => TRUE,
                'attr' =>array(
                    'class='=>'form-group form-control'
                )
            ))
            ->add('firstname',TextType::class,array(
                'label' => 'Prénom',
                'required' => TRUE,
                'attr' =>array(
                    'class='=>'form-group form-control'
                )
            ))


            ;

        }

        public function getParent(){
            return BaseRegistrationFormType::class;
        }

        public function getBlockPrefix()
        {
            return 'app_user_registration';
        }
    }

// 配置\services.yaml


    # This file is the entry point to configure your own services.
    # Files in the packages/ subdirectory configure your dependencies.

    # Put parameters here that don't need to change on each machine where the app is deployed
    # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
    parameters:
        locale: 'fr'

    services:

        app.form.registration:
            class: AppBundle\Form\RegistrationFormType
            tags:
                - { name: form.type }    
                - { firstname: form.type }        

        # default configuration for services in *this* file
        _defaults:
            autowire: true      # Automatically injects dependencies in your services.
            autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

        # makes classes in src/ available to be used as services
        # this creates a service per class whose id is the fully-qualified class name
        App\:
            resource: '../src/*'
            exclude: '../src/{DependencyInjection,Entity,EventListener,Migrations,Tests,Kernel.php}'

        # controllers are imported separately to make sure services can be injected
        # as action arguments even if you don't extend any base controller class
        App\Controller\:
            resource: '../src/Controller'
            tags: ['controller.service_arguments']

// 配置\fos_user.yaml

    fos_user:
        db_driver: orm
        user_class: App\Entity\User
        firewall_name: main
        service:
            mailer: fos_user.mailer.noop
        from_email:
            address: "sebvrg@gmail.com"
            sender_name: "sebvrg@gmail.com"
        registration: 
            form:
                type: AppBundle\Form\RegistrationFormType

// 在控制台输出:

    In FileLoader.php line 166:

      A "tags" entry is missing a "name" key for service "app.form.registration" in C:\Users\sebvr\Desktop\Projets\SELT\selt\config/services.yaml in C:\Users\sebvr\Deskt
      op\Projets\SELT\selt\config/services.yaml (which is loaded in resource "C:\Users\sebvr\Desktop\Projets\SELT\selt\config/services.yaml").


    In YamlFileLoader.php line 489:

      A "tags" entry is missing a "name" key for service "app.form.registration" in C:\Users\sebvr\Desktop\Projets\SELT\selt\config/services.yaml.

标签: symfony-4.2

解决方案


您是否也扩展了您的用户实体?

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
 * @ORM\Entity
 * @ORM\Table(name="`user`")
 */
class User extends BaseUser
{

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $firstname;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    }
}

您是否在 app/config/config.yml 中注册了表单?

fos_user:
    registration:
        form:
            type: AppBundle\Form\RegistrationFormType

您的标签也显示 Symfony 4.2,但您使用的是 symfony 3 (AppBundle) 而不是 (App) 的结构


推荐阅读