Symfony2 - Sonata adminbundle 菜单不会显示

Posted

技术标签:

【中文标题】Symfony2 - Sonata adminbundle 菜单不会显示【英文标题】:Symfony2 - Sonata adminbundle menu won't show 【发布时间】:2013-10-27 22:09:55 【问题描述】:

.. 怎么了伙计们,我使用 FOSUSer Bundel 和 PUGXMultiUser Bundle 使用 Symfony 2.3.5 做这个项目,我想使用 SonataAdmin Bundle,但是在配置之后,我在仪表板中看不到这个关于菜单的问题..这是你认为我错过了什么的配置!!

config.yml

    imports:
        -  resource: parameters.yml 
        -  resource: security.yml 
        -  resource: @PokTVBundle/Resources/config/admin.yml 

    framework:
        #esi:             ~
        translator:       fallback: %locale% 
        secret:          %secret%
        router:
            resource: "%kernel.root_dir%/config/routing.yml"
            strict_requirements: ~
        form:            ~
        csrf_protection: ~
        validation:       enable_annotations: true 
        templating:
            engines: ['twig']
            #assets_version: SomeVersionScheme
        default_locale:  "%locale%"
        trusted_proxies: ~
        session:         ~
        fragments:       ~
        http_method_override: true


    # Twig Configuration
    twig:
        debug:            %kernel.debug%
        strict_variables: %kernel.debug%

    # Assetic Configuration
    assetic:
        debug:          %kernel.debug%
        use_controller: false
        bundles:        [ ]
        #java: /usr/bin/java
        filters:
            cs-s-rewrite: ~
            #closure:
            #    jar: %kernel.root_dir%/Resources/java/compiler.jar
            #yui_css:
            #    jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar

    # Doctrine Configuration
    doctrine:
        dbal:
            driver:   %database_driver%
            host:     %database_host%
            port:     %database_port%
            dbname:   %database_name%
            user:     %database_user%
            password: %database_password%
            charset:  UTF8
            # if using pdo_sqlite as your database driver, add the path in parameters.yml
            # e.g. database_path: %kernel.root_dir%/data/data.db3
            # path:     %database_path%

        orm:
            auto_generate_proxy_classes: %kernel.debug%
            auto_mapping: true

    # Swiftmailer Configuration
    swiftmailer:
        transport: %mailer_transport%
        host:      %mailer_host%
        username:  %mailer_user%
        password:  %mailer_password%
        spool:      type: memory 

    #FOSBundle
    fos_user:
        db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
        firewall_name: main
        user_class: POKEtudiant\UserBundle\Entity\User
        service:
            mailer: fos_user.mailer.twig_swift
            user_manager: pugx_user_manager
        #registration:

            #confirmation:
                #enabled:    true
                #from_email:
                    #address:        noreply@POKEtudiant.com
                    #sender_name:    Elamrani Abou Elassad

    #PUGXBundle
    pugx_multi_user:
      users:
        etudiants:
            entity: 
              class: POKEtudiant\UserBundle\Entity\Etudiant
    #          factory: 
            registration:
              form: 
                type: POKEtudiant\UserBundle\Form\Type\RegistrationEtudiantFormType
                name: fos_user_registration_form
                validation_groups:  [etudiant_grp, Default]
              template: UserBundle:Registration:etudiants.form.html.twig
            profile:
              form:
                type: POKEtudiant\UserBundle\Form\Type\ProfileEtudiantFormType
                name: fos_user_profile_form
                #validation_groups:  [Profile, Default] 
        recruteurs:
            entity: 
              class: POKEtudiant\UserBundle\Entity\Recruiter
            registration:
              form: 
                type: POKEtudiant\UserBundle\Form\Type\RegistrationRecruiterFormType
              template: UserBundle:Registration:recruteurs.form.html.twig
            profile:
              form: 
                type: POKEtudiant\UserBundle\Form\Type\ProfileRecruiterFormType

    #SonataBlock
    sonata_block:
        default_contexts: [cms]
        blocks:
            sonata.admin.block.admin_list:
                contexts:   [admin]

            #sonata.admin_doctrine_orm.block.audit:
            #    contexts:   [admin]

            sonata.block.service.text:
            sonata.block.service.rss:

            # Some specific block from the SonataMediaBundle
            #sonata.media.block.media:
            #sonata.media.block.gallery:
            #sonata.media.block.feature_media:

    #SonataKnp
    knp_menu:
        twig:  # use "twig: false" to disable the Twig extension and the TwigRenderer
            template: knp_menu.html.twig
        templating: false # if true, enables the helper for php templates
        default_renderer: twig # The renderer to use, list is also available by default
    #SonataAdmin
    sonata_admin:
        title:      PokTv
        #title_logo: /bundles/acmedemo/fancy_acme_logo.png
        #security:
            #handler: sonata.admin.security.handler.noop
            #acl_user_manager: fos_user.user_manager
        templates:
            # default global templates
            layout:  SonataAdminBundle::standard_layout.html.twig
            ajax:    SonataAdminBundle::ajax_layout.html.twig
            dashboard: SonataAdminBundle:Core:dashboard.html.twig

            # default actions templates, should extend a global templates
            list:    SonataAdminBundle:CRUD:list.html.twig
            show:    SonataAdminBundle:CRUD:show.html.twig
            edit:    SonataAdminBundle:CRUD:edit.html.twig
            history: SonataAdminBundle:CRUD:history.html.twig
            preview: SonataAdminBundle:CRUD:preview.html.twig
            delete:  SonataAdminBundle:CRUD:delete.html.twig
            batch:   SonataAdminBundle:CRUD:list__batch.html.twig
            batch_confirmation: SonataAdminBundle:CRUD:batch_confirmation.html.twig
            # list related templates
            inner_list_row: SonataAdminBundle:CRUD:list_inner_row.html.twig
            base_list_field: SonataAdminBundle:CRUD:base_list_field.html.twig

            # default values of block templates, they should extend the base_block template
            list_block: SonataAdminBundle:Block:block_admin_list.html.twig
        dashboard:

            blocks:
                -  position: left, type: sonata.admin.block.admin_list 
                # display two dashboard blocks
                #-
                    #position: left
                    #type: sonata.admin.block.admin_list
                    #settings:
                        #groups: [Movies]
                #-
                    #position: right
                    #type: sonata.admin.block.admin_list
                    #settings:
                        #groups: [Series]
            groups:
                Movies:
                    items:
                        - sonata.admin.movies
                Series:
                    items:
                        - sonata.admin.series
                Foot:
                    items:
                        - sonata.admin.foot
    sonata_doctrine_orm_admin:
        # default value is null, so doctrine uses the value defined in the configuration
        entity_manager: 

        templates:
            form:
                - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
            filter:
                - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
            types:
                list:
                    array:      SonataAdminBundle:CRUD:list_array.html.twig
                    boolean:    SonataAdminBundle:CRUD:list_boolean.html.twig
                    date:       SonataAdminBundle:CRUD:list_date.html.twig
                    time:       SonataAdminBundle:CRUD:list_time.html.twig
                    datetime:   SonataAdminBundle:CRUD:list_datetime.html.twig
                    text:       SonataAdminBundle:CRUD:base_list_field.html.twig
                    trans:      SonataAdminBundle:CRUD:list_trans.html.twig
                    string:     SonataAdminBundle:CRUD:base_list_field.html.twig
                    smallint:   SonataAdminBundle:CRUD:base_list_field.html.twig
                    bigint:     SonataAdminBundle:CRUD:base_list_field.html.twig
                    integer:    SonataAdminBundle:CRUD:base_list_field.html.twig
                    decimal:    SonataAdminBundle:CRUD:base_list_field.html.twig
                    identifier: SonataAdminBundle:CRUD:base_list_field.html.twig

                show:
                    array:      SonataAdminBundle:CRUD:show_array.html.twig
                    boolean:    SonataAdminBundle:CRUD:show_boolean.html.twig
                    date:       SonataAdminBundle:CRUD:show_date.html.twig
                    time:       SonataAdminBundle:CRUD:show_time.html.twig
                    datetime:   SonataAdminBundle:CRUD:show_datetime.html.twig
                    text:       SonataAdminBundle:CRUD:base_show_field.html.twig
                    trans:      SonataAdminBundle:CRUD:show_trans.html.twig
                    string:     SonataAdminBundle:CRUD:base_show_field.html.twig
                    smallint:   SonataAdminBundle:CRUD:base_show_field.html.twig
                    bigint:     SonataAdminBundle:CRUD:base_show_field.html.twig
                    integer:    SonataAdminBundle:CRUD:base_show_field.html.twig
                    decimal:    SonataAdminBundle:CRUD:base_show_field.html.twig            

admin.yml

    services:
        sonata.admin.movies:
            class: POKEtudiant\PokTVBundle\Admin\MoviesAdmin
            tags:
                -  name: sonata.admin, manager_type: orm, group: "Movies", label: "Movies", show_in_dashboard: "true" 
            arguments:
                - ~
                - POKEtudiant\PokTVBundle\Entity\Movies
                - ~
        sonata.admin.series:
            class: POKEtudiant\PokTVBundle\Admin\SeriesAdmin
            tags:
                -  name: sonata.admin, manager_type: orm, group: "Series", label: "Series" 
            arguments:
                - ~
                - POKEtudiant\PokTVBundle\Entity\Series
                - ~
        sonata.admin.foot:
            class: POKEtudiant\PokTVBundle\Admin\FootAdmin
            tags:
                -  name: sonata.admin, manager_type: orm, group: "Foot", label: "Foot" 
            arguments:
                - ~
                - POKEtudiant\PokTVBundle\Entity\Foot
                - ~

app/config/routing.yml

    #PokTV Bundle
    pok_tv:
        resource: "@PokTVBundle/Resources/config/routing.yml"
        prefix:   /PokTV

    #User Bundle
    user:
        resource: "@UserBundle/Resources/config/routing.yml"
        prefix:   /

    #Sonata Admin
    admin:
        resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
        prefix: /admin

    _sonata_admin:
        resource: .
        type: sonata_admin
        prefix: /admin

appKernel (registerBundles)

    public function registerBundles()
        
            $bundles = array(
                new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
                new Symfony\Bundle\SecurityBundle\SecurityBundle(),
                new Symfony\Bundle\TwigBundle\TwigBundle(),
                new Symfony\Bundle\MonologBundle\MonologBundle(),
                new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
                new Symfony\Bundle\AsseticBundle\AsseticBundle(),
                new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
                new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),


                new FOS\UserBundle\FOSUserBundle(),
                new PUGX\MultiUserBundle\PUGXMultiUserBundle(),

                new Sonata\BlockBundle\SonataBlockBundle(),
                new Sonata\jQueryBundle\SonatajQueryBundle(),
                new Knp\Bundle\MenuBundle\KnpMenuBundle(),
                new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
                new Sonata\AdminBundle\SonataAdminBundle(),

                new POKEtudiant\UserBundle\UserBundle(),
                new POKEtudiant\PokTVBundle\PokTVBundle(),
            );

            if (in_array($this->getEnvironment(), array('dev', 'test'))) 
                $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
                $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
                $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
                $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            

            return $bundles;
        

【问题讨论】:

【参考方案1】:

您似乎没有为您的用户配置 ROLE_SONATA_ADMIN; 或者只是在security.yml中映射它,像这样

security:
    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]

在您的用户拥有此角色后,必须显示菜单;

【讨论】:

【参考方案2】:

检查您的 twig 文件以查看仍在使用的块。奏鸣曲块部分只有 sonata.admin.block.admin_list 应该加载默认值。

您可以通过启用过滤器来获得错误反馈: [对不起,这是我的第一反应。你如何让格式化工作?] 奏鸣曲块: default_contexts:[cms] 例外: 默认: 过滤器:debug_only 渲染器:抛出 过滤器: debug_only:奏鸣曲.block.exception.filter.debug_only ignore_block_exception:奏鸣曲.block.exception.filter.ignore_block_exception keep_all:奏鸣曲.block.exception.filter.keep_all keep_none:奏鸣曲.block.exception.filter.keep_none 渲染器: 内联:sonata.block.exception.renderer.inline inline_debug:奏鸣曲.block.exception.renderer.inline_debug 抛出:sonata.block.exception.renderer.throw

然后在 admin 块下直接添加这一行: 块: 奏鸣曲.admin.block.admin_list: 上下文:[管理员] 例外:过滤器:keep_all,渲染器:内联

【讨论】:

感谢您的回答,我发现我没有将 ROLE_SONATA_ADMIN 添加到 role_hierarchy 中。

以上是关于Symfony2 - Sonata adminbundle 菜单不会显示的主要内容,如果未能解决你的问题,请参考以下文章

Symfony2、Sonata、UserBundle:当用户启用更新时发送电子邮件

Symfony2,Sonata MediaBundle:向表中添加新字段

Symfony2 - Sonata adminbundle 菜单不会显示

在 symfony2 sonata 管理包中设置默认值

Symfony2 覆盖 Sonata 媒体图像提供程序。(将接口传递给构造函数)

Symfony2 Sonata Media Bundle -- 上传图片时出错