gpt4 book ai didi

symfony - 用 fosuser 覆盖注册 Controller 无法 Autowiring 服务 "App\Controller\RegistrationController"

转载 作者:行者123 更新时间:2023-12-04 01:59:06 25 4
gpt4 key购买 nike

我试图覆盖 FosUserBundle 注册表 Controller ,但出现以下错误:


无法 Autowiring 服务“App\Controller\RegistrationController”:方法的参数“$formFactory”
“FOS\UserBundle\Controller\RegistrationController::__construct()”
引用接口(interface)“FOS\UserBundle\Form\Factory\FactoryInterface”但不存在此类服务。
您也许应该将此接口(interface)别名为这些现有服务之一:“fos_user.profile.form.factory”、“fos_user.registration.form.factory”、“fos_user.change_password.form.factory”、
“fos_user.resetting.form.factory”。您是否创建了一个实现此接口(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()
));
}


}
?>

最佳答案

我遇到了同样的问题,SF4 和 FOSUserBundle。环顾了半天后,我提出了解决方案。我使用了@thibaut 的一些 Material 并将其改编为 SF 4.2。

我注入(inject)fos用户注册controlleur需要的服务,给FOS\UserBundle\Form\Factory\FactoryInterface取个别名

配置/服务.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

然后在我的注册 Controller 中,我重新声明一个构造函数并保存要在 Controller 中使用的 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 - 用 fosuser 覆盖注册 Controller 无法 Autowiring 服务 "App\Controller\RegistrationController",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48590862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com