多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
1、新建并激活Bundle【AppBundle】 ~~~ php app/console generate:bundle ~~~ `编辑:app/AppKernel.php` ~~~ <?php public function registerBundles() { return array( #…… new AppBundle\AppBundle(), ); } ~~~ 2、新建Twig模板 `新建:src/AppBundle/Resources/Views/Block/test.html.twig` ~~~ {% extends "SonataBlockBundle:Block:block_base.html.twig" %} {% block block %} <script src="http://code.highcharts.com/4.0.1/highcharts.js"></script> <script src="http://code.highcharts.com/4.0.1/modules/exporting.js"></script> <script type="text/javascript"> $(function () { $(document).ready(function () { // Build the chart $('#container').highcharts({ chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: 'Browser market shares January, 2015 to May, 2015' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: false }, showInLegend: true } }, series: [{ name: "Brands", colorByPoint: true, {#data: {% autoescape false %}{{ data }}{% endautoescape %}#} data: {{ data|raw }} }] }); }); }); </script> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> {% endblock block %} ~~~ 3、新建BlockService类 `新建:src/AppBundle/Block/ExampleBlockService.php` ~~~ <?php namespace AppBundle\Block; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolver; use Sonata\BlockBundle\Block\BaseBlockService; use Sonata\BlockBundle\Block\BlockContextInterface; use Ob\HighchartsBundle\Highcharts\Highchart; class TestBlockService extends BaseBlockService { private $em; public function entityManager($entityManager) { $this->em = $entityManager; } public function configureSettings(OptionsResolver $resolver) { $resolver->setDefaults( array( 'url' => false, 'title' => 'app.block.service.test', 'template' => 'AppBundle:Block:test.html.twig' ) ); } public function execute(BlockContextInterface $blockContext, Response $response = null) { $y = date("Y"); $m = date("m"); $from = new \DateTime($y."-".$m."-1"); $m == 12 ? $to = new \DateTime(($y+1)."-"."1-1") : $to = new \DateTime($y."-".($m+1)."-1"); $qb = $this->em->createQueryBuilder(); $qr = $qb->from("AppBundle\Entity\Press", "p") ->select("s.sourcename, count(p.id) as cnt") ->join("p.source", "s") ->where("p.createdAt BETWEEN :from AND :to") ->groupBy("s.id") ->setParameter("from", $from) ->setParameter("to", $to) ->getQuery() ->getResult(); foreach($qr as $k=>$v){ $arr = array_values($v); $data[$k] = new stdClass(); $data[$k]->name = $arr[0]; $data[$k]->y = (float)$arr[1]; } $params = array( 'context' => $blockContext, 'settings' => $blockContext->getSettings(), 'block' => $blockContext->getBlock(), 'data' => json_encode($data), ); return $this->renderResponse($blockContext->getTemplate(), $params, $response); } } ~~~ 4、将AppBundle\Block\TestBlockService定义为服务 `新建:src/AppBundle/resources/config/services.yml` ~~~ services: app.block.service.test: class: AppBundle\Block\TestBlockService tags: - { name: sonata.block } calls: - [ entityManager, [ @doctrine.orm.entity_manager ] ] arguments: [ app.block.service.test, @templating ] ~~~ `编辑:app/config/config.yaml` ~~~ imports: #…… - { resources: "@AppBundle/Resources/config/services.yml" } ~~~ 5、将app.block.service.test配置到admin首页 `编辑:app/config/sonata.yml` ~~~ sonata.block: default_contexts: [ cms ] blocks: app.block.service.test: contexts: [ admin ] sonata.admin: #…… dashboard: blocks: - { position: center, type: app.block.service.test } ~~~