I'm currently trying to override a controller from FOSUserBundle. In the new documentation, (https://symfony.com/doc/3.4/bundles/override.html), they said we just have to override the route name of the bundle.
I've tried something like this :
<?php
namespace App\Controller\Bundles;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class RegistrationController extends BaseController {
/**
* @Route("/registration/", name="fos_user_registration_register")
* @param Request $request
*
* @return Response
*/
public function registerAction(Request $request) {
// My code
}
But it does not work when I am rendering the FOS register form :
{{ render(controller('FOSUserBundle:Registration:register')) }}
This was working with Sf3.3 :(
My solution - works fine from Symfony 4.1 by decorate FOS controller service.
Firstly - decorate selected controller (docs: https://symfony.com/doc/current/service_container/service_decoration.html)
inside
App\Controller\ResettingController:
decorates: fos_user.resetting.controller
arguments:
- '@App\Controller\ResettingController.inner'
- '@event_dispatcher'
- '@fos_user.resetting.form.factory'
- '@fos_user.user_manager'
- '@fos_user.util.token_generator'
- '@fos_user.mailer'
- '%fos_user.resetting.retry_ttl%'
- '@security.csrf.token_manager'
Now controller which will override original ResettingController:
<?php
namespace App\Controller;
use FOS\UserBundle\Controller\ResettingController as BaseResettingController;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
class ResettingController extends AbstractController
{
/**
* @var BaseResettingController
*/
private $resettingController;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var FactoryInterface
*/
private $formFactory;
/**
* @var UserManagerInterface
*/
private $userManager;
/**
* @var TokenGeneratorInterface
*/
private $tokenGenerator;
/**
* @var MailerInterface
*/
private $mailer;
/**
* @var string
*/
private $retryTtl;
/**
* @var CsrfTokenManagerInterface
*/
private $csrfTokenManager;
/**
* ResettingController constructor.
*
* @param BaseResettingController $resettingController
* @param EventDispatcherInterface $eventDispatcher
* @param FactoryInterface $formFactory
* @param UserManagerInterface $userManager
* @param TokenGeneratorInterface $tokenGenerator
* @param MailerInterface $mailer
* @param $retryTtl
*/
public function __construct(
BaseResettingController $resettingController,
EventDispatcherInterface $eventDispatcher,
FactoryInterface $formFactory,
UserManagerInterface $userManager,
TokenGeneratorInterface $tokenGenerator,
MailerInterface $mailer,
$retryTtl,
CsrfTokenManagerInterface $csrfTokenManager
)
{
$this->resettingController = $resettingController;
$this->eventDispatcher = $eventDispatcher;
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->tokenGenerator = $tokenGenerator;
$this->mailer = $mailer;
$this->retryTtl = $retryTtl;
$this->csrfTokenManager = $csrfTokenManager;
}
public function requestAction()
{
return $this->resettingController->requestAction();
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function sendEmailAction(Request $request)
{
return $this->resettingController->sendEmailAction($request);
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function checkEmailAction(Request $request)
{
return $this->resettingController->checkEmailAction($request);
}
/**
* @param string $token
* @return null|RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function resetAction(Request $request, string $token)
{
// custom reset action
}
}
Follow the official documentation here : https://symfony.com/doc/3.4/bundles/override.html#controllers
To override, I find the documentation too ambigus, you have to add in your routing configuration, BEFORE FOS routing declaration, a route with the same path but NOT the same route ID :
fos_user_security_login_override:
path: /login
defaults: { _controller: SecurityAppBundle:Security:login }
#FOS routing declaration comes AFTER
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With