Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

symfony - SonataAdminBundle : display non crud (statistics)

I'm using sonata admin bundle to generate my backend, I'm so happy with it that I would like to use my backend to display statistics as well.

I guess I can do that by tweaking bundle's views, "standard_layout.html.twig" maybe.

Problem is, I can't find examples or even people speaking about it, so I'm wondering, is that possible ? Aren't people speaking about it because it's too simple ? Did you do it ?

I really would like to have a single backend, so pls enlighten me !

Thank you, copndz

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it`s possible. It can be done with Sonata Block or using your own controller.

If you use your controller, you can overload (one or more) actions from default CRUD controller and how the rendered result will look like depends on you.

  1. Replace default controller SonataAdminBundle:CRUD with your controller AcmeDemoAdminBundle:ProductStatisticsAdmin in definition of your admin service and remove entity because we will try to render our statistics without CRUD operations.

    <service id="acme_demo_admin.product_statistics" class="AcmeBundleDemoAdminBundleAdminProductStatisticsAdmin">
        <tag name="sonata.admin" manager_type="orm" group="statistics_group" label_catalogue="admin" label="Product Statistics" />
        <argument />
        <argument />
        <argument>AcmeDemoAdminBundle:ProductStatisticsAdmin</argument>
    </service>
    
  2. Create admin service ProductStatisticsAdmin in Acme/Bundle/DemoAdminBundle/Admin/ProductStatisticsAdmin.php. The class will be very simple, because we will need only list action and no other CRUD operation.

    <?php
    namespace AcmeBundleDemoAdminBundleAdmin;
    
    use SonataAdminBundleAdminAdmin;
    use SonataAdminBundleRouteRouteCollection;
    
    class ProductStatisticsAdmin extends Admin
    {
        protected $baseRoutePattern = 'product-statistics';
        protected $baseRouteName = 'productStatistics';
    
        protected function configureRoutes(RouteCollection $collection)
        {
            $collection->clearExcept(array('list'));
        }
    }
    
  3. Create your controller ProductStatisticsAdminController in Acme/Bundle/DemoAdminBundle/Controller/ProductStatisticsAdminController.php and overload listAction() from Sonata`s CRUDController. Inside this action you can call your DB and retrieve statistics and then render them with your template.

    <?php
    
    namespace AcmeBundleDemoAdminBundleController;
    
    use SonataAdminBundleControllerCRUDController as Controller;
    use SymfonyComponentSecurityCoreExceptionAccessDeniedException;
    
    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. Create template product_statistics.html.twig to generate graphs and display statistics in 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 %}
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...