📜 ⬆️ ⬇️

Non-entity page in SonataAdminBundle

The main task of SonataAdminBundle is to create a configurable interface for editing the ORM / ODM entity. However, at some point it may be necessary to create an arbitrary page in the administrative interface that is not associated with any entity, so that the design of the new page fully fits into the design of the SonataAdminBundle pages and, accordingly, place a link to it on the main page.

For an experienced Symfony 2 developer, the task is not particularly difficult, but for beginners it can be a lot of trouble (as was the case in my case).

Before the start


It is assumed that you have installed and configured SonataAdminBundle:


The routes spelled something like
/app/config/routing.yml
sonata_page_cache: resource: '@SonataCacheBundle/Resources/config/routing/cache.xml' prefix: / admin: resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml' prefix: /admin _sonata_admin: resource: . type: sonata_admin prefix: /admin 


Step 1. Create a controller class


Create the controller class /src/Acme/DemoBundle/Controller/AdminController.php , which is inherited from the base kernel controller kernel class of the administrative interface:
 <?php namespace Acme\DemoBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Sonata\AdminBundle\Controller\CoreController; class AdminController extends CoreController { public function custompageAction(Request $request) { //      // ... return $this->render('AcmeDemoBundle:Admin:custompage.html.twig', array( 'base_template' => $this->getBaseTemplate(), 'admin_pool' => $this->container->get('sonata.admin.pool'), 'blocks' => $this->container->getParameter('sonata.admin.configuration.dashboard_blocks') )); } } 

The custompageAction method will be responsible for handling the new page.
')

Step 2. Adding a route


Add a route for our new page, which will be available at / admin / custompage, to the file /app/config/routing.yml :

 sonata_admin_custompage: pattern: /admin/custompage defaults: { _controller: AcmeDemoBundle:Admin:custompage } 

Step 3. Create a page template


Create a template file for the new page /src/Acme/DemoBundle/Resources/views/Admin/custompage.html.twig with the following content:
 {% extends 'SonataAdminBundle::standard_layout.html.twig' %} {% block breadcrumb %} <li><a href="{{ url('sonata_admin_dashboard') }}">{% trans %}Dashboard{% endtrans %}</a><span class="divider">/</span></li> <li class="active">Custom Page</li> {% endblock %} {% block content %} <h1>Custom Page</h1> Custom content {% endblock %} 

In this template, we extend the main administrative interface template SonataAdminBundle :: standard_layout.html.twig ( /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Resources/views/standard_layout.html.twig ), form bread crumbs ( breadrchr block ) and place, in fact, the contents of the new page in the content block.

Thus, the new page is available at / admin / custompage:


Step 4. Adding a link to the main page


Now, to place the link on the main page of the administrative interface / admin / dashboard, you need to override the dashboard.html.twig template, which in SonataAdminBundle is responsible for displaying the content of the main page. To do this, create the file /app/Resources/SonataAdminBundle/views/Core/dashboard.html.twig :

 {% extends base_template %} {% block title %}{{ 'title_dashboard'|trans({}, 'SonataAdminBundle') }}{% endblock%} {% block breadcrumb %}{% endblock %} {% block content %} <div class="row-fluid"> <div class="span6"> <a href="{{ url('sonata_admin_custompage') }}" class="btn">Custom Page</a> </div> </div> <br/> <div class="row-fluid"> <div class="span6"> {% for block in blocks %} {% if block.position == 'left' %} {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }} {% endif %} {% endfor %} </div> <div class="span6"> {% for block in blocks %} {% if block.position == 'right' %} {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }} {% endif %} {% endfor %} </div> </div> {% endblock %} 

The next step is to clear the cache (even if you are working in a dev environment):
 php app/console cache:clear --env=dev --no-debug 

As a result, we have a link button on the main page / admin / dashboard:


Conclusion


This problem can certainly be solved in different ways. Perhaps you offer a more elegant solution, so we share comments and suggestions in the comments to the topic.

You can read about creating an arbitrary page for an entity here .

Source: https://habr.com/ru/post/167543/


All Articles