问题
I tried to use the FOSUserBundle, I followed the instructions on the documentation for overwriting the Bundle, but I get this error when I try to access to /register while /login works (I didn't overwrite it):
Could not load type "app_user_registration"
500 Internal Server Error - InvalidArgumentException
Configurations
Symfony version: 3.1.7
FOSUserBundle version: dev-master
My files
app/config/services.yml:
services:
app.form.registration:
class: CoreBundle\Form\Type\RegistrationFormType
tags:
- { name: form.type, alias: app_user_registration }
app/config/config.yml:
fos_user:
db_driver: orm
firewall_name: main
user_class: CoreBundle\Entity\User
registration:
form:
type: app_user_registration
src/CoreBundle/Form/Type/RegistrationFormType.php
<?php
// src/CoreBundle/Form/Type/RegistrationFormType.php
namespace CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function getParent()
{
return 'fos_user_registration';
}
public function getName()
{
return 'app_user_registration';
}
}
?>
src/viwa/UserBundle/Controller/RegistrationController.php:
<?php
// src/viwa/UserBundle/Controller/RegistrationController.php
namespace viwa\UserBundle\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
{
// Don't need to change this right now.
}
?>
src/viwa/UserBundle/viwaUserBundle.php:
<?php
// src/viwa/UserBundle/viwaUserBundle.php
namespace viwa\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class viwaUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
If you need anything other to help me I edit my post.
Hope anyone can help me out.
回答1:
Your config.yml file should be:
fos_user:
db_driver: orm
firewall_name: main
user_class: CoreBundle\Entity\User
registration:
form:
type: CoreBundle\Form\Type\RegistrationFormType
In your src/CoreBundle/Form/Type/RegistrationFormType.php
, getParent()
function should be:
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
回答2:
You probably read documentation for '1.3.x / current' which is open by default. If you switch to '2.0 / master' you will se the correct version of documentation.
回答3:
I know it's an old question but as I found it searching for the same problem, I share my solution, like the accepted answer, but more conform to the suggested/official syntax :) I have upgraded to symfony 3.4, and FosUserBundle 2.1, now you need to return the FQCN as explain in the UPGRADE 2.x to 3.0
Returning type instances from FormTypeInterface::getParent() is not supported anymore. Return the fully-qualified class name of the parent type class instead.
Before:
class MyType
{
public function getParent()
{
return new ParentType();
}
}
After:
class MyType
{
public function getParent()
{
return ParentType::class;
}
}
In this case:
public function getParent()
{
return RegistrationFormType::class;
}
Don't forget the use on the top of the file:
use FOS\UserBundle\Form\Type\RegistrationFormType;
来源:https://stackoverflow.com/questions/41824546/fosuserbundle-overwrite-registration-but-could-not-load-type-error