2
namespace Acme\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use FOS\UserBundle\Model\UserManagerInterface;

class LessonAdmin extends Admin
{
    public function
    {
         //I have tried these, but in vain.
         $items = $this->container->getParameter('items');
         or 
         $items = $this->getContainer()->getParameter('items');

I think this problem is related with Dependency Injection though, still unclear for me. How can I inject getContainer item here??

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • check [this link](http://stackoverflow.com/questions/12056178/how-to-access-service-container-in-symfony2-global-helper-function-service),hopes help you.. – Leo Silence Apr 08 '14 at 03:21

2 Answers2

4

In SonataAdmin, the DI container can be fetched from the Admin configuration pool:

<?php
use Sonata\AdminBundle\Admin\Admin;

class YourAdmin extends Admin
{

    protected function yourAdminMethod()
    {
        $this->getConfigurationPool()->getContainer()->getParameter('your_parameter');
    }
}

`

kix
  • 3,290
  • 27
  • 39
0

You can also inject parameters through setter in admin service definition:

services:
    sonata.admin.lesson:
        class: Acme\AdminBundle\Admin\LessonAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, ...}
        arguments:
            - ~
            - Acme\AppBundle\Entity\Lesson
            - ~
        calls:
           - [ setItems, ["%items%"]]

And in your admin class:

class LessonAdmin extends Admin
{
    public function setItems($items)
    {
        $this->items = $items;
    }

In this way your parameter is accessible in your whole admin class.

Tsounabe
  • 2,109
  • 1
  • 16
  • 25