Symfony 5 & EasyAdmin 3.0 - /admin route not found

Viewed 12763

I have fresh Symfony 5 project instlled locally, and added Easy Admin trought Symfony CLI:

symfony composer req admin

I should have /admin route but it's missing

I run:

symfony console cache:clear

symfony composer dump-autoload

rm -rf var/cache/*

symfony console debug:router
 -------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _preview_error             ANY      ANY      ANY    /_error/{code}.{_format}           
  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open                    
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  homepage                   ANY      ANY      ANY    /                                  
 -------------------------- -------- -------- ------ ----------------------------------- 
// config/routes/easy_admin.yaml

easy_admin_bundle:
    resource: '@EasyAdminBundle/Controller/EasyAdminController.php'
    prefix: /admin
    type: annotation
symfony console router:match /admin

                                                                                                                       
 [ERROR] None of the routes match the path "/admin"

What am I missing?

10 Answers

Yeah, I am reading this book right now and ran into the same issue.

First, make sure that your working directory is clean (run "git status" and remove all changes made by EasyAdminBundle setup).

Then run:

composer require easycorp/easyadmin-bundle:2.*

to install EasyAdminBundle version 2; with this version, you can proceed as described in the book.

As others have already said you need to create a dashboard with:

php bin/console make:admin:dashboard

And then create at least one crud controller. Since it seems you're using the Fast Track book, you need to type the following command twice and make one for both Comment and Conference:

php bin/console make:admin:crud

I am using the Fast Track book too and this is how my files ended up. I am still learning easy admin3 and thus make no claims as to best practice, but this should make the menus look as they do in the Fast Track screenshots:

src/Controller/Admin/DashboardController.php:

/**
 * @Route("/admin", name="admin")
 */
public function index(): Response
{
    $routeBuilder = $this->get(CrudUrlGenerator::class)->build();

    return $this->redirect($routeBuilder->setController(ConferenceCrudController::class)->generateUrl());
}

public function configureDashboard(): Dashboard
{
    return Dashboard::new()
        ->setTitle('Guestbook');
}

public function configureMenuItems(): iterable
{
    yield MenuItem::linktoRoute('Back to website', 'fa fa-home', 'homepage');
    yield MenuItem::linkToCrud('Conference', 'fa fa-map-marker', Conference::class);
    yield MenuItem::linkToCrud('Comment', 'fa fa-comment', Comment::class);
}

src/Controller/Admin/CommentCrudController.php:

public static function getEntityFqcn(): string
{
    return Comment::class;
}

public function configureFields(string $pageName): iterable
{
    return [
        IdField::new('id')->hideOnForm(),
        TextField::new('author'),
        TextareaField::new('text')->hideOnIndex(), // Removing ->hideOnIndex() will display a link to a text modal
        EmailField::new('email'),
        DateTimeField::new('createdAt')->hideOnForm(),
        ImageField::new('photoFilename', 'Photo')->setBasePath('/uploads/photos')->hideOnForm(),

        AssociationField::new('conference')
    ];
}

src/Controller/Admin/ConferenceCrudController.php

public static function getEntityFqcn(): string
{
    return Conference::class;
}

public function configureFields(string $pageName): iterable
{
    return [
        IdField::new('id')->hideOnForm(),
        TextField::new('city'),
        TextField::new('year'),
        BooleanField::new('isInternational'),
        IntegerField::new('commentCount', 'Comments')->hideOnForm()
    ];
}

And in src/Entity/Conference.php I added the following to make commentCount available:

public function getCommentCount(): int
{
    return $this->comments->count();
}

To generate the createdAt datetime automatically when the comment is submitted, I first installed the following bundle:

$ composer require stof/doctrine-extensions-bundle

And then modified config/packages/stof_doctrine_extensions.yaml:

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            tree: true
            timestampable: true

And finally decorated private $createdAt in src/Entity/Comment.php with the following:

/**
 * @var \DateTime
 * @ORM\Column(type="datetime")
 * @Gedmo\Mapping\Annotation\Timestampable(on="create")
 * @Doctrine\ORM\Mapping\Column(type="datetime")
 */
private $createdAt;

When migrating from easyadmin 2 to 3, it appears that the route name is not preserved. One way to do this is in DashboardController, add

/**
 * @Route("/admin", name="easyadmin")
 */
public function index(): Response
{
    return parent::index();
}

I solved this problem as follows:

  1. Remove EasyAdmin 3
composer remove admin
  1. Install additional package.
composer require "easycorp/easyadmin-bundle":"^2.3"
  1. Update packages.
composer update

In my case

first

composer remove doctrine/common

and then

 composer require easycorp/easyadmin-bundle v2.3.9 doctrine/common v2.13.3 doctrine/persistence v1.3.8

With that it worked for the book

For me, the clearest and most complete explanation is the answer to @AnnaHowell I would only change a part of your code. In src/Controller/Admin/CommentCrudController.php:

 public function configureFields(string $pageName): iterable
{
    $avatar = ImageField::new('photoFilename')->setBasePath('uploads/photos/')->setLabel('Photo');
    $avatarTextFile = TextField::new('photoFilename');
   
     {
        yield     TextField::new('author');
        yield     TextEditorField::new('text');
        yield     TextField::new('state');
        yield     EmailField::new('email');
        yield     DateTimeField::new('createdAt', 'Created')->setFormat('dd-MM-y HH:mm:ss')
                ->setSortable(true)->setFormTypeOption('disabled','disabled');
        if (Crud::PAGE_INDEX === $pageName) {
        yield ImageField::new('photoFilename')->setBasePath('uploads/photos/')->setLabel('Photo');
    } elseif (Crud::PAGE_EDIT === $pageName) {
       yield TextField::new('photoFilename')->setLabel('Photo');
    }      
       
};

Thus, we allow the Administrator to not only evaluate the text, but also the relevance of the photo. What if a comment has great text, but an inconvenient or trivial photo? The Administrator could delete the name of the photo (and that way it would not be visible), leave the text comment and publish it.

Simply copy all of /vendor/easycorp/easyadmin-bundle/src/Resources/public to public/bundles/easyadmin

Short answer:

Make sure you are using HTTPS to access the admin route. Even if the admin route is supposed to use any scheme in the debug:router command.

Detailed answer

Same situation, reproducible with Symfony 4.4 and Symfony 5.2 and Easyadmin 3.2

I found out that I was accessing my server with http (WAMP):

URL Result
http://localhost 200
http://localhost/admin 404

Then I tried with

symfony console server:start

Noticed the warning about installing the certificate, installed it and ran again the symfony server.

After than I was able to use HTTPS, and the admin was accessible on https://localhost/admin

Related