简体   繁体   中英

Unable to generate a URL for the named route “admin_app_sonatauseruser_create” after renaming SonataUserUser entity

My application worked fine until I decided to rename SonataUserUser to User and SonataUserUserGroup to group.

This is how my user entity looks like

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;
    
    .....

    public function __construct()
    {
        parent::__construct();
    }

    public function __toString()
    {
        return $this->getFirstName() === null ? 'New' : $this->getFullname();
    }
}

App\Admin\UserAdmin

declare(strict_types=1);

namespace App\Admin;

use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\Form\Type\DatePickerType;
use Sonata\UserBundle\Form\Type\SecurityRolesType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;

class UserAdmin extends BaseUserAdmin
{
    protected $translationDomain = 'SonataBundle';


    /**
     * {@inheritdoc}
     */
    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $showMapper
            ->with('General')
                ->add('username')
                ->add('email')
            ->end()
            ->with('Groups')
                ->add('groups')
            ->end()
            ->with('Profile')
                ->add('dateOfBirth')
                ->add('firstname')
                ->add('lastname')
                ->add('website')
                ->add('biography')
                ->add('gender')
                ->add('locale')
                ->add('timezone')
                ->add('phone')
            ->end()
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper): void
    {
        // define group zoning
        $formMapper
            ->tab('Details')
                ->with('Profile', ['class' => 'col-md-6'])->end()
                ->with('General', ['class' => 'col-md-6'])->end()
                ->with('Social', ['class' => 'col-md-6'])->end()
            ->end();

        $now = new \DateTime();

        $genderOptions = [
            'choices' => \call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
            'required' => true,
            'translation_domain' => $this->getTranslationDomain(),
        ];

        $formMapper
            ->tab('Details')
                ->with('General')
                    ->add('username')
                    ->add('email')
                    ->add('plainPassword', PasswordType::class, [
                        'required' => (!$this->getSubject() || null === $this->getSubject()->getId()),
                        'label' => 'Password',
                        'help' => 'Updating this field will change your password.'
                    ])
                ->end()
                ->with('Profile')
                    ->add('firstname', null, ['required' => false])
                    ->add('lastname', null, ['required' => false])
                    ->add('dateOfBirth', DatePickerType::class, [
                        'required' => false,
                    ])
                    ->add('gender', ChoiceType::class, $genderOptions)
                    ->add('locale', LocaleType::class, ['required' => false])
                    ->add('timezone', TimezoneType::class, ['required' => false])
                    ->add('phone', null, ['required' => false])
                ->end()
            ->end();
    }
}

config/routes/sonata_admin.yaml

admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

config/packages/sonata_user

sonata_user:
    security_acl: false
    manager_type: orm
    class:
        user: App\Entity\User
        group: App\Entity\Group

    mailer: sonata.user.mailer.default

    impersonating:
        route: sonata_admin_dashboard

    table:
        user_group: "users_groups"

The error I get is An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_app_sonatausergroup_create" as such route does not exist."). in in vendor/sonata-project/admin-bundle/src/Resources/views/Block/block_admin_list.html.twig (line 28) .

I'd appreciate any form of help

What does php bin/console debug:router return? You could find the route name that is being used I would expect. Example output from one that I have in development:

  dashboard                   ANY      ANY      ANY    /account/dashboard
  home                        ANY      ANY      ANY    /
  app_register                ANY      ANY      ANY    /register
  app_login                   ANY      ANY      ANY    /login
  app_logout                  ANY      ANY      ANY    /logout

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