覆盖FOSUserBundle Symfony中的RegistrationController

时间:2014-03-05 21:12:35

标签: symfony fosuserbundle

我需要在我的捆绑中将regitration控制器覆盖1。

我在userbundle中创建了这个文件,但它给出了我在stackoverflow上看到很多但没有回复的错误。

错误是

  

ContextErrorException:运行时注意:TA \ UserBundle \ Controller \ RegistrationController :: registerAction()的声明应与E中的FOS \ UserBundle \ Controller \ RegistrationController :: registerAction(Symfony \ Component \ HttpFoundation \ Request $ request)兼容: \ php \ xampp \ htdocs \ platform \ src \ TA \ UserBundle \ Controller \ RegistrationController.php第9行

控制器

<?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(),
        ));
    }
}

即使我改变了

 public function registerAction()  

public function registerAction(Request $request)

错误成了

  

ContextErrorException:运行时注意:TA \ UserBundle \ Controller \ RegistrationController :: registerAction()的声明应与E中的FOS \ UserBundle \ Controller \ RegistrationController :: registerAction(Symfony \ Component \ HttpFoundation \ Request $ request)兼容: \ php \ xampp \ htdocs \ platform \ src \ TA \ UserBundle \ Controller \ RegistrationController.php第9行

2 个答案:

答案 0 :(得分:1)

这个对我有用。我的例子:

<?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;

    }
}

基本上你需要在'registerAction'函数中添加请求,参数'Request $ request',并将'request'参数传递给父'registerAction'函数

答案 1 :(得分:0)

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

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