创建子管理员
假设你有一个 PlaylistAdmin
和一个 VideoAdmin
。你可以选择性地声明 VideoAdmin
为 PlaylistAdmin
的子级。这将创建新的路由,例如,/playlist/{id}/video/list
,其中视频将自动按帖子过滤。
要做到这一点,你首先需要在你的 PlaylistAdmin
服务配置中调用 addChild
方法,并传入两个参数,子管理员名称(在本例中是 VideoAdmin
服务)以及关联我们的子实体与其父级的实体字段。
1 2 3 4 5 6 7 8 9 10
# config/services.yaml
App\Admin\VideoAdmin:
# tags, calls, etc
App\Admin\PlaylistAdmin:
calls:
- [addChild, ['@App\Admin\VideoAdmin', 'playlist']]
# Or `[addChild, ['@App\Admin\VideoAdmin']]` if there is no
# field to access the Playlist from the Video entity
要显示 VideoAdmin
,请在你的 PlaylistAdmin
类中扩展菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
namespace App\Admin;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
final class PlaylistAdmin extends AbstractAdmin
{
protected function configureTabMenu(MenuItemInterface $menu, string $action, ?AdminInterface $childAdmin = null): void
{
if (!$childAdmin && !in_array($action, ['edit', 'show'])) {
return;
}
$admin = $this->isChild() ? $this->getParent() : $this;
$id = $admin->getRequest()->get('id');
$menu->addChild('View Playlist', $admin->generateMenuUrl('show', ['id' => $id]));
if ($this->isGranted('EDIT')) {
$menu->addChild('Edit Playlist', $admin->generateMenuUrl('edit', ['id' => $id]));
}
if ($this->isGranted('LIST')) {
$menu->addChild('Manage Videos', $admin->generateMenuUrl('App\Admin\VideoAdmin.list', ['id' => $id]));
}
}
}
也可以设置点分隔的值,例如 post.author
,如果你的父级和子级管理员不是直接相关的。
请注意,作为子管理员是可选的,这意味着无论你是否真的需要,都会创建常规路由。要摆脱它们,你可以覆盖 configureRoutes
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
final class VideoAdmin extends AbstractAdmin
{
protected function configureRoutes(RouteCollectionInterface $collection): void
{
if ($this->isChild()) {
return;
}
// This is the route configuration as a parent
$collection->clear();
}
}
你可以根据需要深度嵌套管理员。
假设你想向视频添加评论。
然后,你可以将你的 CommentAdmin
管理服务添加为 VideoAdmin
管理服务的子级。
最后,管理界面将如下所示

这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。