我试图覆盖FosUserBundle注册表控制器,但出现以下错误:
Cannot autowire service "App\Controller\RegistrationController": argument "$formFactory" of method
"FOS\UserBundle\Controller\RegistrationController::__construct()"
references interface "FOS\UserBundle\Form\Factory\FactoryInterface" but no such service exists.
You should maybe alias this interface to one of these existing services: "fos_user.profile.form.factory", "fos_user.registration.form.factory", "fos_user.change_password.form.factory",
"fos_user.resetting.form.factory". Did you create a class that implements this interface?
我正在使用Symfony4,这是我的RegistrationController.php
代码。我尝试了多种方法,但我找不到让它工作的方法。
class RegistrationController extends BaseController
{
public function registerAction(Request $request)
{
$form = $this->get('fos_user.registration.form.factory');
$formHandler = $this->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->getParameter('fos_user.registration.confirmation.enabled');
die("Hello");
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
if ($confirmationEnabled) {
$this->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
// $this->authenticateUser($user);
$route = 'users_edit';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->get('router')->generate($route, array("id" => $user->id));
return new RedirectResponse($url);
}
return $this->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
'form' => $form->createView()
));
}
}
?>
在symfony 4.2工作
services.yaml
App\Controller\RegistrationController:
arguments:
$formFactory: '@fos_user.registration.form.factory'
和控制器
use FOS\UserBundle\Controller\RegistrationController as Base;
class RegistrationController extends Base
{
private $eventDispatcher;
private $formFactory;
private $userManager;
private $tokenStorage;
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage)
{
parent::__construct($eventDispatcher, $formFactory,$userManager, $tokenStorage);
$this->eventDispatcher = $eventDispatcher;
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->tokenStorage = $tokenStorage;
}
我有同样的问题,我通过公开“fos_user.resetting.form.factory”来解决它
MailerInterface
然后使用具有新名称的服务
FOS\UserBundle\Form\Factory\FactoryInterface: '@fos_user.resetting.form.factory'
FOS\UserBundle\Mailer\MailerInterface: '@fos_user.mailer.default'
App\Controller\RegistrationController:
autowire: true
arguments:
$formFactory: '@fos_user.registration.form.factory'
我有同样的问题,SF4和FOSUserBundle。看了半天后,我找到了解决方案。我使用@thibaut中的一些材料并将其调整为SF 4.2。
我注入fos用户注册controlleur所需的服务,并给FOS \ UserBundle \ Form \ Factory \ FactoryInterface一个别名
配置/ services.yaml
app.controller.register:
class: App\Controller\bundles\FOSUserBundle\RegistrationController
arguments:
$eventDispatcher: '@event_dispatcher'
$formFactory: '@fos_user.registration.form.factory'
$userManager: '@fos_user.user_manager'
$tokenStorage: 'security.token_storage'
calls:
- method: setContainer
arguments:
- '@service_container'
public: true
FOS\UserBundle\Form\Factory\FactoryInterface:
alias: 'fos_user.registration.form.factory'
public: true
然后在我的注册控制器中,我重新声明一个构造函数并保存formfactory以在控制器中使用
应用\控制器\束\ FOSUserBundle
protected $formFactory;
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage, $serviceContainer=null)
{
$this->setContainer($serviceContainer);
$this->formFactory = $formFactory;
parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);
}
在我的行动中使用declare $ formfactory
/**
* @Route("/register", name="registration")
*/
public function registerAction(Request $request)
{
/** @var $formFactory FactoryInterface */
$formFactory = $this->formFactory;
/** @var $userManager UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
/** @var $dispatcher EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
希望它也能帮到你们
我不知道你使用哪个版本的FOSUserBundle
但是还没有官方对sf4
的支持 - 请参阅release notes。您可以尝试dev-master
版本并按照this issue使其工作。
您应该使用钩子并避免在FOSUserBundel v2.x中覆盖控制器。您可以在页面上阅读更多信息:http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html
我不得不使用拒绝在我的config/services.yaml
中自动装配的FOS服务的别名
FOS\UserBundle\Form\Factory\FormFactory: '@fos_user.registration.form.factory'
正如vendor/friendsofsymfony/user-bundle/Resources/config/registration.xml
所见
您好我想我找到了解决此问题的方法:定义您的服务:
app.controller.register:
class: AppBundle\Controller\RegisterController
arguments: ['@service_container']
public: true
然后你的控制器
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
/**
* @Route(service="app.controller.register")
*/
class RegisterController extends BaseController
{
public function __construct($serviceContainer=null)
{
$this->setContainer($serviceContainer);
$eventDispatcher=$this->container->get('event_dispatcher');
$formFactory=$this->container->get('fos_user.registration.form.factory');
$userManager=$this->container->get('fos_user.user_manager');
$tokenStorage=$this->container->get('security.token_storage');
parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);
}
/**
* @Route("/register", name="register")
*/
public function registerAction(Request $request)
{
//do your stuff
}
}
希望这个帮助(对不起英语不好)
要在覆盖中解决此问题:
public function __construct() {
$this->eventDispatcher = $this->get('event_dispatcher');
$this->formFactory = $this->get('fos_user.registration.form.factory');
$this->userManager = $this->get('fos_user.user_manager');
$this->tokenStorage = $this->get('security.token_storage'); }
瞧!您只是在构造函数中调用这些服务,而不是将它们作为参数传递到注册控制器服务中。
我现在这样做了,它适用于sf3.4。
我有同样的问题,SF4,FOSUserBundle(在SonataAdmin之后)。我想听一个事件:注册确认,向管理员发送消息。我的第一次尝试:覆盖FosUser控制器。但问题很多!感谢@majne,我去了http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html,即使我不是'master',也可以从FOS \ UserBundle \ FOSUserEvents类中挂钩事件。我有一些问题要聪明地调整使用声明......
使用Symfony 4.2,以下内容应该可行。
与其他答案中的建议相反,无需扩展相应的FOSUserBundle控制器或修改__construct
方法。
/config/services.压马路
FOS\UserBundle\Form\Factory\FactoryInterface: '@fos_user.registration.form.factory'
在我的情况下,我实际上覆盖了qazxsw poi和qazxsw poi,在这种情况下,需要连接两种形式 - 以及RegistrationController
- 如下:
/config/services.压马路
ResettingController