用 fosuser 覆盖注册控制器无法自动装配服务 "App\Controller\RegistrationController"
Override Registration Controller with fosuser Cannot autowire service "App\Controller\RegistrationController"
我试图覆盖 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()
));
}
}
?>
我不知道您使用的 FOSUserBundle
是哪个版本,但还没有对 sf4
的官方支持 - 请参阅 release notes. You can try dev-master
version and follow this issue 使其正常工作。
我遇到了同样的问题,我通过 "fos_user.resetting.form.factory" public
解决了
#fos_user.yaml
services:
new_service_name:
alias: fos_user.registration.form.factory
public: true
然后使用新名称的服务
$form = $this->get('new_service_name');
您应该使用挂钩并避免在 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
}
}
希望对您有所帮助(抱歉英语不好)
要在覆盖中解决此问题:
- 完全覆盖 FOSUserBundle RegistrationController,即将其全部内容复制到 AppBundle/Controller/RegistrationController。php
然后将 __construct 函数替换为以下内容:
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 class 的事件。
我在清楚地调整 use 语句时遇到了一些问题...
我遇到了同样的问题,SF4 和 FOSUserBundle。环顾了半天后,我提出了解决方案。我使用了来自@thibaut 的一些 material 并将其改编为 SF 4.2。
我注入fos用户注册控制器需要的服务,给FOS\UserBundle\Form\Factory\FactoryInterface一个别名
config/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
App\Controller\bundles\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');
希望对大家也有帮助
在 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;
}
对于 Symfony 4.2,以下应该可以工作。
与其他答案中的建议相反,无需扩展相应的 FOSUserBundle 控制器或修改 __construct
方法。
/config/services.yaml
FOS\UserBundle\Form\Factory\FactoryInterface: '@fos_user.registration.form.factory'
在我的例子中,我实际上覆盖了 RegistrationController
和 ResettingController
,在这种情况下,需要连接两种形式 - 以及 MailerInterface
- 如下:
/config/services.yaml
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'
我试图覆盖 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()
));
}
}
?>
我不知道您使用的 FOSUserBundle
是哪个版本,但还没有对 sf4
的官方支持 - 请参阅 release notes. You can try dev-master
version and follow this issue 使其正常工作。
我遇到了同样的问题,我通过 "fos_user.resetting.form.factory" public
解决了#fos_user.yaml
services:
new_service_name:
alias: fos_user.registration.form.factory
public: true
然后使用新名称的服务
$form = $this->get('new_service_name');
您应该使用挂钩并避免在 FOSUserBundel 中覆盖控制器 v2.x 您可以在页面上阅读更多信息:http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html
我不得不为拒绝在 config/services.yaml
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
}
}
希望对您有所帮助(抱歉英语不好)
要在覆盖中解决此问题:
- 完全覆盖 FOSUserBundle RegistrationController,即将其全部内容复制到 AppBundle/Controller/RegistrationController。php
然后将 __construct 函数替换为以下内容:
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 class 的事件。 我在清楚地调整 use 语句时遇到了一些问题...
我遇到了同样的问题,SF4 和 FOSUserBundle。环顾了半天后,我提出了解决方案。我使用了来自@thibaut 的一些 material 并将其改编为 SF 4.2。
我注入fos用户注册控制器需要的服务,给FOS\UserBundle\Form\Factory\FactoryInterface一个别名
config/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
App\Controller\bundles\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');
希望对大家也有帮助
在 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;
}
对于 Symfony 4.2,以下应该可以工作。
与其他答案中的建议相反,无需扩展相应的 FOSUserBundle 控制器或修改 __construct
方法。
/config/services.yaml
FOS\UserBundle\Form\Factory\FactoryInterface: '@fos_user.registration.form.factory'
在我的例子中,我实际上覆盖了 RegistrationController
和 ResettingController
,在这种情况下,需要连接两种形式 - 以及 MailerInterface
- 如下:
/config/services.yaml
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'