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??

1
  • check this link,hopes help you.. Commented Apr 8, 2014 at 3:21

2 Answers 2

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');
    }
}

`

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,its perfect and easy solution
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.