-2

I have the given error:

The controller must return a "Symfony\Component\HttpFoundation\Response" object
but it returned an object of type EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu.
namespace App\Controller\User;

use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\Response;

class DashboardUserController extends AbstractDashboardController
{
    /**
     * @Route("/userPanel", name="userPanel")
     */
    public function configureUserMenu(UserInterface $user): UserMenu
    {
        // Usually it's better to call the parent method because that gives you a
        // user menu with some menu items already created ("sign out", "exit impersonation", etc.)
        // if you prefer to create the user menu from scratch, use: return UserMenu::new()->...
        return parent::configureUserMenu($user)
            // use the given $user object to get the user name
            ->setName($user->getUsername())
            // use this method if you don't want to display the name of the user
            ->displayUserName(false)
            // you can use any type of menu item, except submenus
            ->addMenuItems([
                MenuItem::linkToRoute('My Profile', 'fa fa-id-card', '...', ['...' => '...']),
                MenuItem::linkToRoute('Settings', 'fa fa-user-cog', '...', ['...' => '...']),
                MenuItem::section(),
                MenuItem::linkToLogout('Logout', 'fa fa-sign-out'),
            ]);
    }
}

yolenoyer
  • 8,797
  • 2
  • 27
  • 61

1 Answers1

0

The first problem I see is that you have a route associated with configureUserMenu(). Routes are supposed to return some type of response. Your dashboard controller should have an index() function that has a route associated. There is where you would return a twig template. Also you only need 1 dashboard controller for EasyAdmin 3.

If you are wanting to be able to create/read/update/delete an entity, then you will need to make a Crud Controller. I advise you to read the EasyAdmin docs as it's actually quite easy to have this generated for you using a command. Here is an Dashboard Controller example for you, please note there are a lot of functions your dashboard controller can override in AbstractDashboardController. All this info is explained in decent detail in the docs.

<?php
namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

class DashboardController extends AbstractDashboardController
{
    /**
     * @Route("/admin", name="admin")
     */
    public function index(): Response
    {
        $templateVars = [];
        return $this->render('admin/dashboard.html.twig',$templateVars);
    }

    public function configureUserMenu(UserInterface $user): UserMenu
    {
        $userMenuItems = [
            MenuItem::linkToUrl('Profile','fa-id-card','/admin/profile'),
            MenuItem::linkToUrl('Settings','fa-user-cog','/admin/settings'),
            MenuItem::linkToLogout('__ea__user.sign_out', 'fa-sign-out')
        ];

        if ($this->isGranted(Permission::EA_EXIT_IMPERSONATION)) {
            $userMenuItems[] = 
            MenuItem::linkToExitImpersonation(
                '__ea__user.exit_impersonation', 
                'fa-user-lock'
            );
        }

        return UserMenu::new()
            ->displayUserName()
            ->displayUserAvatar()
            ->setName(method_exists($user, '__toString') ? (string) $user : $user->getUsername())
            ->setAvatarUrl(null)
            ->setMenuItems($userMenuItems);
    }
}
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
IndyDevGuy
  • 126
  • 5