php - EasyAdminBundle, configureUserMenu interface dont let me return
Get the solution ↓↓↓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'),
]);
}
}
Answer
Solution:
The first problem I see is that you have a route associated withconfigureUserMenu()
. Routes are supposed to return some type of response. Your dashboard controller should have anindex()
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 inAbstractDashboardController
. 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);
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your system folder path does not appear to be set correctly. please open the following file and correct this: index.php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.