douhuang75397 2017-04-11 19:57 采纳率: 0%
浏览 42
已采纳

Symfony3 FossUserBundle从另一个控制器呈现注册表单,以便通过ajax注册

On my Symfony 3 project I use FossUserBundle and what I want to acheive is to render the extended form I developed for Registration page to another controller WITHOUT redirect.

In other words what I want to achieve is to make /register routes to be called via Ajax and on / to render the form. Therefore I want to do the first step and try to render the form in / instead of /register but all the post actions will be done in /register

Therefore I tried the following:

I have changed the Appbundle class as:

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

And tried on AppBundle\Controller\Pages class the following:

1.To render the form without extending the FOS\UserBundle\Controller\RegistrationController;

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;


/**
 * @author pcmagas
 * Class that loads miscellanous pages
 */
class PagesController extends Controller
{
    /**
     *@Route("/", name="index")
     *@Method("GET")
     */
    function homePage()
    {
        $form = $this->get('fos_user.registration.form');

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

2.To extend the FOS\UserBundle\Controller\RegistrationController and try to render the form:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

/**
 * @author pcmagas
 * Class that loads miscellanous pages
 */
class PagesController extends BaseController
{
    /**
     *@Route("/", name="index")
     *@Method("GET")
     */
    function homePage()
    {
        $form = $this->get('fos_user.registration.form');

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

Both of these methods fail to render the form (just render it remember the /register route will handle the form when submited via ajax.) and return the following error message:

You have requested a non-existent service "fos_user.registration.form". Did you mean one of these: "fos_user.registration.form.factory", "fos_user.registration.form.type"?

Do you have any sort of Idea how to do that?

Edit 1

I tried:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

/**
 * @author pcmagas
 * Class that loads miscellanous pages
 */
class PagesController extends BaseController
{
    /**
     *@Route("/", name="index")
     *@Method("GET")
     */
    function homePage()
    {
        $form = $this->get('fos_user.registration.form.type');

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

With error:

Attempted to call an undefined method named "createView" of class "FOS\UserBundle\Form\Type\RegistrationFormType".

Also similar result with:

Attempted to call an undefined method named "createView" of class "FOS\UserBundle\Form\Factory\FormFactory".

  • 写回答

1条回答 默认 最新

  • dongyo1818 2017-04-11 22:58
    关注

    In the end this is what worked for me:

    namespace AppBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    
    /**
     * @author pcmagas
     * Class that loads miscellanous pages
     */
    class PagesController extends Controller
    {
        /**
         *@Route("/", name="index")
         *@Method("GET")
         */
        function homePage()
        {
            $form = $this->get('fos_user.registration.form.factory');
            $form = $form->createForm();
    
            return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
                         'form' => $form->createView(),
                 ));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?