@@ -319,10 +319,10 @@ The main menu is a collection of objects implementing
319319``EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemInterface `` that configure
320320the look and behavior of each menu item::
321321
322- use App\Entity\BlogPost ;
323- use App\Entity\Category ;
324- use App\Entity\Comment ;
325- use App\Entity\User ;
322+ use App\Controller\Admin\BlogPostCrudController ;
323+ use App\Controller\Admin\CategoryCrudController ;
324+ use App\Controller\Admin\CommentCrudController ;
325+ use App\Controller\Admin\UserCrudController ;
326326 use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminDashboard;
327327 use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
328328 use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
@@ -338,12 +338,12 @@ the look and behavior of each menu item::
338338 MenuItem::linkToDashboard('Dashboard', 'fa fa-home'),
339339
340340 MenuItem::section('Blog'),
341- MenuItem::linkToCrud( 'Categories', 'fa fa-tags', Category::class ),
342- MenuItem::linkToCrud( 'Blog Posts', 'fa fa-file-text', BlogPost::class ),
341+ MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags'),
342+ MenuItem::linkTo(BlogPostCrudController::class, 'Blog Posts', 'fa fa-file-text'),
343343
344344 MenuItem::section('Users'),
345- MenuItem::linkToCrud( 'Comments', 'fa fa-comment', Comment::class ),
346- MenuItem::linkToCrud( 'Users', 'fa fa-user', User::class ),
345+ MenuItem::linkTo(CommentCrudController::class, 'Comments', 'fa fa-comment'),
346+ MenuItem::linkTo(UserCrudController::class, 'Users', 'fa fa-user'),
347347 ];
348348 }
349349 }
@@ -390,13 +390,73 @@ The rest of options depend on each menu item type, as explained in the next sect
390390Menu Item Types
391391~~~~~~~~~~~~~~~
392392
393+ Controller Menu Item
394+ ....................
395+
396+ This is the most common menu item type. Use ``MenuItem::linkTo() `` to link to
397+ any admin controller: CRUD controllers, Dashboard controllers, or custom
398+ controllers with the ``#[AdminRoute] `` attribute.
399+
400+ The first argument is the FQCN *(fully-qualified class name) * of the controller.
401+ The label and icon are optional (when linking to a CRUD controller without a
402+ label, EasyAdmin derives it automatically from the entity name)::
403+
404+ use App\Controller\Admin\CategoryCrudController;
405+ use App\Controller\Admin\LegacyCategoryCrudController;
406+ use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
407+ use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
408+
409+ public function configureMenuItems(): iterable
410+ {
411+ return [
412+ // ...
413+
414+ // links to the 'index' action of the Category CRUD controller
415+ // the label is auto-derived from the entity name (e.g. "Categories")
416+ MenuItem::linkTo(CategoryCrudController::class),
417+
418+ // you can pass an explicit label and icon
419+ MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags'),
420+
421+ // links to a different CRUD action
422+ MenuItem::linkTo(CategoryCrudController::class, 'Add Category', 'fa fa-tags')
423+ ->setAction(Action::NEW),
424+
425+ MenuItem::linkTo(CategoryCrudController::class, 'Show Main Category', 'fa fa-tags')
426+ ->setAction(Action::DETAIL)
427+ ->setEntityId(40585),
428+
429+ // uses custom sorting options for the listing
430+ MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags')
431+ ->setDefaultSort(['createdAt' => 'DESC']),
432+ ];
433+ }
434+
435+ You can also link to your own Symfony controllers if they use the
436+ ``#[AdminRoute] `` attribute to integrate them in EasyAdmin::
437+
438+ use App\Controller\Admin\AnalyticsDashboardController;
439+
440+ MenuItem::linkTo(AnalyticsDashboardController::class, 'Analytics', 'fa fa-chart-line');
441+
442+ If the controller is invokable (has a ``__invoke() `` method), the action is
443+ detected automatically. Otherwise, call ``->setAction('theActionName') `` to
444+ specify which action to link to.
445+
393446CRUD Menu Item
394447..............
395448
396- This is the most common menu item type and it links to some action of some
397- :doc: `CRUD controller </crud >`. Instead of passing the FQCN *(fully-qualified
398- class name) * of the CRUD controller, you must pass the FQCN of the Doctrine
399- entity associated to the CRUD controller::
449+ .. note ::
450+
451+ ``MenuItem::linkToCrud() `` is still fully supported, but ``MenuItem::linkTo() ``
452+ (explained above) is now the recommended way to create menu items that link to
453+ CRUD controllers. ``linkTo() `` provides a unified API that works with any type
454+ of admin controller and puts the controller class first, making the code
455+ easier to navigate and refactor.
456+
457+ ``MenuItem::linkToCrud() `` links to some action of some :doc: `CRUD controller </crud >`.
458+ Instead of passing the FQCN of the CRUD controller, you pass the FQCN of the
459+ Doctrine entity associated to the CRUD controller::
400460
401461 use App\Entity\Category;
402462 use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
@@ -446,6 +506,16 @@ have to specify the route name (it's found automatically)::
446506 ];
447507 }
448508
509+ .. tip ::
510+
511+ You can also use ``MenuItem::linkTo() `` to link to a dashboard. This is
512+ especially useful when you have multiple dashboards and want to link to a
513+ *different * one::
514+
515+ use App\Controller\Admin\AnalyticsDashboardController;
516+
517+ MenuItem::linkTo(AnalyticsDashboardController::class, 'Analytics', 'fa fa-chart-line');
518+
449519Route Menu Item
450520...............
451521
@@ -558,15 +628,18 @@ Submenus
558628The main menu can display up to two level nested menus. Submenus are defined
559629using the ``subMenu() `` item type::
560630
631+ use App\Controller\Admin\BlogPostCrudController;
632+ use App\Controller\Admin\CategoryCrudController;
633+ use App\Controller\Admin\CommentCrudController;
561634 use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
562635
563636 public function configureMenuItems(): iterable
564637 {
565638 return [
566639 MenuItem::subMenu('Blog', 'fa fa-article')->setSubItems([
567- MenuItem::linkToCrud( 'Categories', 'fa fa-tags', Category::class ),
568- MenuItem::linkToCrud( 'Posts', 'fa fa-file-text', BlogPost::class ),
569- MenuItem::linkToCrud( 'Comments', 'fa fa-comment', Comment::class ),
640+ MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags'),
641+ MenuItem::linkTo(BlogPostCrudController::class, 'Posts', 'fa fa-file-text'),
642+ MenuItem::linkTo(CommentCrudController::class, 'Comments', 'fa fa-comment'),
570643 ]),
571644 // ...
572645 ];
@@ -591,8 +664,8 @@ generator to return the menu items::
591664
592665 if ('... some complex expression ...') {
593666 yield MenuItem::section('Blog');
594- yield MenuItem::linkToCrud( 'Categories', 'fa fa-tags', Category::class );
595- yield MenuItem::linkToCrud( 'Blog Posts', 'fa fa-file-text', BlogPost::class );
667+ yield MenuItem::linkTo(CategoryCrudController::class, 'Categories', 'fa fa-tags');
668+ yield MenuItem::linkTo(BlogPostCrudController::class, 'Blog Posts', 'fa fa-file-text');
596669 }
597670
598671 // ...
0 commit comments