【问题标题】:SonataAdminBundle : display non crud (statistics)SonataAdminBundle :显示非粗略(统计)
【发布时间】:2013-04-04 16:45:40
【问题描述】:

我正在使用 Sonata 管理包来生成我的后端,我对此非常满意,因此我也想使用我的后端来显示统计数据。

我想我可以通过调整 bundle 的视图来做到这一点,也许是“standard_layout.html.twig”。

问题是,我找不到例子,甚至找不到人谈论它,所以我想知道,这可能吗?人们谈论它不是因为它太简单了吗?你做到了吗?

我真的很想有一个后端,所以请赐教!

谢谢你, 无名氏

【问题讨论】:

    标签: symfony sonata-admin symfony-2.2


    【解决方案1】:

    是的,有可能。可以使用 Sonata Block 或使用您自己的控制器来完成。

    如果您使用 控制器,您可以从默认 CRUD 控制器重载(一个或多个)操作,渲染结果的外观取决于您。

    1. 用您的控制器替换默认控制器SonataAdminBundle:CRUD AcmeDemoAdminBundle:ProductStatisticsAdmin 在您的管理服务的定义和删除实体,因为我们将尝试在没有 CRUD 操作的情况下呈现我们的统计信息。

      <service id="acme_demo_admin.product_statistics" class="Acme\Bundle\DemoAdminBundle\Admin\ProductStatisticsAdmin">
          <tag name="sonata.admin" manager_type="orm" group="statistics_group" label_catalogue="admin" label="Product Statistics" />
          <argument />
          <argument />
          <argument>AcmeDemoAdminBundle:ProductStatisticsAdmin</argument>
      </service>
      
    2. 创建管理服务 ProductStatisticsAdmin in Acme/Bundle/DemoAdminBundle/Admin/ProductStatisticsAdmin.php。该类将非常简单,因为我们只需要 list 操作,不需要其他 CRUD 操作。

      <?php
      namespace Acme\Bundle\DemoAdminBundle\Admin;
      
      use Sonata\AdminBundle\Admin\Admin;
      use Sonata\AdminBundle\Route\RouteCollection;
      
      class ProductStatisticsAdmin extends Admin
      {
          protected $baseRoutePattern = 'product-statistics';
          protected $baseRouteName = 'productStatistics';
      
          protected function configureRoutes(RouteCollection $collection)
          {
              $collection->clearExcept(array('list'));
          }
      }
      
    3. 创建您的控制器 ProductStatisticsAdminController 在Acme/Bundle/DemoAdminBundle/Controller/ProductStatisticsAdminController.php 中并从 Sonata 的 CRUDController 重载 listAction()。在此操作中,您可以调用数据库并检索统计信息,然后使用您的模板呈现它们。

      <?php
      
      namespace Acme\Bundle\DemoAdminBundle\Controller;
      
      use Sonata\AdminBundle\Controller\CRUDController as Controller;
      use Symfony\Component\Security\Core\Exception\AccessDeniedException;
      
      class ProductStatisticsAdminController extends Controller
      {
          public function listAction()
          {
              if (false === $this->admin->isGranted('LIST')) {
                  throw new AccessDeniedException();
              }
      
              //... use any methods or services to get statistics data
              $statisticsData = ...
      
             return $this->render('AcmeDemoAdminBundle:ProductStatistics:product_statistics.html.twig', array(
                          'statistics_data'  => $statisticsData,
                      ));
          }
      }
      
    4. 创建模板product_statistics.html.twig生成图表并在Acme/Bundle/DemoAdminBundle/Resources/views/ProductStatistics/product_statistics.html.twig中显示统计数据

      {% extends base_template %}
      
      {% block javascripts %}
          {{ parent() }}
          {# put links to javascript libraries here if you need any #}
      {% endblock %}
      
      {% block content %}
          {# put some html code to display statistics data or use some javascript library to generate cool graphs #}
      {% endblock %}
      

    【讨论】:

    • 我不确定这是否仅在新版本的奏鸣曲管理员中,但我必须将$baseRouteName 添加到管理员类中才能正常工作。
    • 这也适用于实际版本,但它不会在“navbar-left”的顶部添加面包屑。除此之外,做得很好!
    • 工作,但是奏鸣曲搜索给出错误:“在模板的呈现过程中引发了异常(“类不存在”)。”
    【解决方案2】:

    实际上使用块和创建单独的页面有点不同。我认为 OP 正在尝试在奏鸣曲管理员中创建单独的页面。

    1. 创建一个控制器,在routing.yml文件中配置它的路由,如果你想让URL看起来像sonata admin,设置一个与sonata admin的前缀相同的前缀。

    2. 渲染模板。这里有两个技巧。

      首先,您需要扩展奏鸣曲管理员的“布局”模板。如果您在config.yml 中更改了它,请相应地更新代码。 Ref

      {% extends "SonataAdminBundle::standard_layout.html.twig" %}
      

      现在您会看到奏鸣曲管理员的菜单栏和页脚已进入这个新页面。但是菜单是空的。要显示菜单,您需要将 admin_pool 从控制器传递到模板。

      $admin_pool = $this->get('sonata.admin.pool');
      
      return array(
          'admin_pool' => $admin_pool,
          // Other variables to pass to template
      );
      

    【讨论】:

    • 谢谢,这很简单,正是我所需要的!
    • 我收到了这个错误:Variable "admin" does not exist in app/Resources/views/base.html.twig at line 84
    【解决方案3】:

    【讨论】:

    • 不鼓励仅链接的答案,因为它们往往会中断。请将链接的相关部分拉到此答案中,以便信息可用,即使其他站点不可用。
    【解决方案4】:

    既然 pulzarraider 向我们解释了一种方法,我将解释另一种方法。

    块捆绑的方式允许以非常强大的方式自定义仪表板。 你可以同时关注Block bundle doc

    1.在 Copndz\MyBundle\Block\Service 中创建 StatisticsBlockService.php

    我想通过对存储的数据进行数学运算来显示统计数据:我需要

    • 导入EntityManager
    • 为服务添加属性 $em
    • 添加构造函数 __construct,它将调用其父构造函数并使用传入参数的 EntityManager 设置 $em

    namespace Copndz\MyBundle\Block\Service;
    use Symfony\Component\HttpFoundation\Response;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Validator\ErrorElement;
    use Sonata\BlockBundle\Model\BlockInterface;
    use Sonata\BlockBundle\Block\BaseBlockService;
    use Doctrine\ORM\EntityManager;
    
    class StatisticsBlockService extends BaseBlockService
    {
        private $em;
        
        /**
         * {@inheritdoc}
         */
        public function execute(BlockInterface $block, Response $response = null)
        {
            $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
            
            $myentityrepository = $this->em->getRepository('CopndzMyBundle:MyEntity');
            $myentity = $myentityrepository->find('5');
            
            return $this->renderResponse('CopndzMyBundle:Block:block_statistics.html.twig', array(
                'block'     => $block,
                'settings'  => $settings,
                'myentity' => $myentity,   
            ), $response);
        }
    
        /**
         * {@inheritdoc}
         */
        public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
        {
            // TODO: Implement validateBlock() method.
        }
    
        /**
         * {@inheritdoc}
         */
        public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
        {
            $formMapper->add('settings', 'sonata_type_immutable_array', array(
                'keys' => array(
                    array('content', 'textarea', array()),
                )
            ));
        }
    
        /**
         * {@inheritdoc}
         */
        public function getName()
        {
            return 'Text (core)';
        }
    
        /**
         * {@inheritdoc}
         */
        public function getDefaultSettings()
        {
            return array(
                'content' => 'Insert your custom content here',
            );
        }
        
        public function __construct($name, $templating, EntityManager $entityManager)
        {
                parent::__construct($name, $templating);
                $this->em = $entityManager;
        }
    }
    

    2。在 MyBundle\Ressources\config\services.yml 中创建服务

     sonata.block.service.statistics:
          class: Copndz\MyBundle\Block\Service\StatisticsBlockService
          tags:
            - { name: sonata.block }
          arguments:
            - "sonata.block.service.statistics"
            - @templating
            - @doctrine.orm.entity_manager
    

    3.将此服务添加到我的 config.yml 中的 sonata_block

    sonata_block:
        default_contexts: [cms]
        blocks:
            sonata.admin.block.admin_list:
                contexts:   [admin]
    
            sonata.block.service.text:
            sonata.block.service.rss:
            sonata.block.service.statistics:
    

    4.在 Copndz\MyBundle\Ressources\views\Block 中创建模板 block_statistics.html.twig

    {% extends sonata_block.templates.block_base %}
    
    {% block block %}
        {{ myentity.name }}
    {% endblock %}
    

    5.最后在config.yml的admin bundle配置中调用服务

    sonata_admin:
        dashboard:
            blocks:
                # display a dashboard block
                - { position: left, type: sonata.admin.block.admin_list }
                - { position: right, type: sonata.block.service.statistics }
    

    【讨论】:

    【解决方案5】:

    我相信您可以使用 Sonata Admin Bundle 的 Sonata Block Bundle 部分来完成您想要实现的目标。

    Sonata Admin Dashboard http://sonata-project.org/bundles/admin/2-1/doc/reference/dashboard.html的文档

    虽然我自己没有做过。

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多