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
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.
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="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>
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 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'));
}
}
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 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,
));
}
}
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 %}