在 symfony 中将管理员组和项目添加到奏鸣曲管理员
add admin groups and item to sonata admin in symfony
我遵循了 sonata admin 的教程,它确实生成了菜单,但所有内容都在 "default" 组下。我想添加新组,例如 "users"、"events" 等等。
我试着做在 https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_knp_menu.html#extending-the-menu 上找到的那个:
<?php
namespace AppBundle\EventListener;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
/**
* Description of AdminMenuBuilderListener
*
* @author christophe
*/
class AdminMenuBuilderListener {
public function addMenuItems(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
$child = $menu->addChild('reports', array(
'route' => 'admin_app_category_index',
'labelAttributes' => array('icon' => 'fa fa-bar-chart'),
));
$child->setLabel('Daily and monthly reports');
}
}
routing.yml
app:
resource: "@AppBundle/Controller/"
type: annotation
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: /user
admin_area:
resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
services.yml
parameters:
# parameter_name: value
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
app.menu_listener:
class: AppBundle\EventListener\AdminMenuBuilderListener
tags:
- { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
CategoryAdmin.php
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class CategoryAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('name', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('name');
}
}
它实际上抛出了异常:Unable to generate a URL for the named route "admin_app_category_index" as such route does not exist
。我是 symfony 的新手,我不知道在这种情况下路由是如何生成的。
欢迎任何帮助。
实际上,有一种更简单的方法可以实现:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category , group : Blog}
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post, group : Blog }
admin.user:
class: AppBundle\Admin\UserAdmin
arguments: [~, AppBundle\Entity\User, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: List, group : Users }
admin.group:
class: AppBundle\Admin\GroupAdmin
arguments: [~, AppBundle\Entity\group, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Groups, group : Users }
您可以在管理菜单中看到允许添加组的group
。
我遵循了 sonata admin 的教程,它确实生成了菜单,但所有内容都在 "default" 组下。我想添加新组,例如 "users"、"events" 等等。
我试着做在 https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_knp_menu.html#extending-the-menu 上找到的那个:
<?php
namespace AppBundle\EventListener;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
/**
* Description of AdminMenuBuilderListener
*
* @author christophe
*/
class AdminMenuBuilderListener {
public function addMenuItems(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
$child = $menu->addChild('reports', array(
'route' => 'admin_app_category_index',
'labelAttributes' => array('icon' => 'fa fa-bar-chart'),
));
$child->setLabel('Daily and monthly reports');
}
}
routing.yml
app:
resource: "@AppBundle/Controller/"
type: annotation
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: /user
admin_area:
resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
services.yml
parameters:
# parameter_name: value
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
app.menu_listener:
class: AppBundle\EventListener\AdminMenuBuilderListener
tags:
- { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
CategoryAdmin.php
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class CategoryAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('name', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('name');
}
}
它实际上抛出了异常:Unable to generate a URL for the named route "admin_app_category_index" as such route does not exist
。我是 symfony 的新手,我不知道在这种情况下路由是如何生成的。
欢迎任何帮助。
实际上,有一种更简单的方法可以实现:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category , group : Blog}
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post, group : Blog }
admin.user:
class: AppBundle\Admin\UserAdmin
arguments: [~, AppBundle\Entity\User, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: List, group : Users }
admin.group:
class: AppBundle\Admin\GroupAdmin
arguments: [~, AppBundle\Entity\group, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Groups, group : Users }
您可以在管理菜单中看到允许添加组的group
。