简体   繁体   中英

Override RegistrationController in FOSUserBundle Symfony

I need to override the regitration contrller by 1 in my bundle.

I made the file inside userbundle, but it gives that error that I saw a lot at stackoverflow but no reply.

Error is

ContextErrorException: Runtime Notice: Declaration of TA\\UserBundle\\Controller\\RegistrationController::registerAction() should be compatible with FOS\\UserBundle\\Controller\\RegistrationController::registerAction(Symfony\\Component\\HttpFoundation\\Request $request) in E:\\php\\xampp\\htdocs\\platform\\src\\TA\\UserBundle\\Controller\\RegistrationController.php line 9

Controller

<?php

//src\TA\UserBundle\Controller\RegistrationController.php
namespace TA\UserBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{ 
    public function registerAction()
    {
        $form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf('New user registration: %s', $user)
            );

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_user_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_user_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
        ));
    }
}

even when I changed

 public function registerAction()  

to

public function registerAction(Request $request)

error became

ContextErrorException: Runtime Notice: Declaration of TA\\UserBundle\\Controller\\RegistrationController::registerAction() should be compatible with FOS\\UserBundle\\Controller\\RegistrationController::registerAction(Symfony\\Component\\HttpFoundation\\Request $request) in E:\\php\\xampp\\htdocs\\platform\\src\\TA\\UserBundle\\Controller\\RegistrationController.php line 9

This one worked for me. My Example:

<?php
namespace FRMKS\FoodR\FoodApp\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;

class RegistrationController extends BaseController
{
    public function registerAction(Request $request)
    {
        $response = parent::registerAction($request);

        // ... do custom stuff

        return $response;

    }
}

Basically you need to add use of request, parameter 'Request $request' to 'registerAction' function and pass 'request' param to parent 'registerAction' function

您是否尝试将完整的'Symfony \\ Component \\ HttpFoundation \\ Request $ request'放入您的方法定义中?

public function registerAction(Symfony\Component\HttpFoundation\Request $request) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM