我正在使用 SonataUserBundle,我正在尝试覆盖编辑配置文件表单,但我不确定 services.yml 和 config.yml。这是代码。
配置文件类型.php
namespace Application\Sonata\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Sonata\UserBundle\Form\Type\ProfileType as BaseType;
class ProfileType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('ciudad', null, array('label' => 'Ciudad'));
$builder->add('file', 'file', array('required' => false, 'label' => 'Subir Foto'));
}
public function getName()
{
return 'sonata_user_profile';
}
}
ProfileFormHandler.php
<?php
namespace Application\Sonata\UserBundle\Form\Handler;
use Sonata\UserBundle\Model\UserInterface;
use Sonata\UserBundle\Form\Handler\ProfileFormHandler as BaseHandler;
class ProfileFormHandler extends BaseHandler
{
public function process(UserInterface $user)
{
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$nombreArchivoFoto = uniqid().$user->getId() . '-' . $user->getUsername() . '-foto-perfil.jpg';
$user->upload($nombreArchivoFoto);
$this->onSuccess($user);
return true;
}
$this->userManager->reloadUser($user);
}
return false;
}
}
服务.yml
services:
sonata_user.registration.form.type:
class: Application\Sonata\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sonata_user_registration }
sonata_user.profile.form.type:
class: Application\Sonata\UserBundle\Form\Type\ProfileType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sonata_user_profile }
sonata_user.form.handler.profile:
class: Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager"]
scope: request
public: false
配置.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
registration:
form:
type: application_sonata_user_registration
profile:
form:
type: fos_user_profile
handler: fos_user.profile.form.handler.default
name: fos_user_profile_form
validation_groups: [Authentication]
sonata_user:
security_acl: false
class:
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
profile:
form:
type: sonata_user_profile
handler: sonata_user.form.handler.profile
name: sonata_user_profile_form
validation_groups: [Profile]
如果我使用上述设置,我会得到下一个异常
ErrorException:运行时注意:Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler::process() 的声明应与 Sonata\UserBundle\Form\Handler\ProfileFormHandler::process(FOS\UserBundle\Model\UserInterface $user) 兼容在 D:\xampp\htdocs\misplanes.dev\src\Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler.php 第 8 行
如果我更改 services.yml
arguments: ["@sonata_user.profile.form", "@request", "@fos_user.user_manager"]
代替
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager"]
我得到下一个例外
ServiceNotFoundException:服务“sonata.user.profile.form.handler”依赖于不存在的服务“sonata_user.profile.form”。
我真的不知道错误在哪里,我尝试了很多配置,并且阅读了不同的论坛和博客,但我没有找到解决方案。我将非常感谢您的帮助。谢谢