2017-02-28 45 views
0

其他類我想使用環境如何獲得比控制器

通常情況下,我可以得到這樣的environemnt在控制器的環境。

$this->container->get(‘kernel’)->getEnvironment(); 

但是除了控制器之外,我該怎麼做呢?

我的想法是這樣的下面。

製作原班讓環境。

namespace Acme\TopBundle\MyServices; 
use Doctrine\ORM\EntityManager; 

class MyFunc 
{ 
    private $em; 
    private $env; 
    public function __construct(EntityManager $em,$env) 
    { 
     $this->em = $em; 
     $this->env = $env; 
    } 

    public function getEnv(){ 
     return $this->env; 
    } 

然後在config.yml

services: 
    myfunc: 
     class: Acme\TopBundle\MyServices\MyFunc 
     arguments: [@doctrine.orm.entity_manager,"%kernel.environment%"] 

註冊服務的話,例如在管理類我如何能得到環境?

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; 
use Symfony\Component\DependencyInjection\ContainerInterface as Container; 
use Ivory\GoogleMap\Places\AutocompleteType; 
class PlaceCatAdmin extends Admin 
{ 
    public function configureListFields(ListMapper $listMapper) 
    { 
    // I want to get environment here 
+0

避免普通類,比如MYFUNC。如果PlaceCatAdmin真正需要的ENV然後定義一個PlaceCatAdmin服務,並注入它。 – Cerad

回答

1

容器訪問是已經可用通過管理員池管理類。

你可以像這樣檢索:

$this->getConfigurationPool()->getContainer()->getParameter('kernel.environment'); 

參考:

https://github.com/sonata-project/SonataAdminBundle/blob/3.x/Admin/AbstractAdmin.php

/** 
* @return Pool 
*/ 
public function getConfigurationPool() 
{ 
    return $this->configurationPool; 
} 

https://github.com/sonata-project/SonataAdminBundle/blob/3.x/Admin/Pool.php

/** 
    * @var ContainerInterface 
    */ 
    protected $container; 
+0

這很好,謝謝!在我的情況下不是控制器,而是在AbstractAdmin中,因此,configurationPool非常有用。 – whitebear

0

要通過服務不是一個好主意訪問您的環境變量,但如果這樣的任何要求,那麼你可以通過這種方式實現這一目標:

namespace Acme\TopBundle\MyServices; 
use Doctrine\ORM\EntityManager; 

class MyFunc 
{ 
    private $em; 
    private $env; 
    public function __construct(EntityManager $em,$env) 
    { 
     $this->em = $em; 
     $this->env = $env; 
    } 

    public function getEnv(){ 
     return $this->env; 
    } 

服務配置,因爲它是。 然後到上網本中,你管理控制器這樣做:

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; 
use Symfony\Component\DependencyInjection\ContainerInterface as Container; 
use Ivory\GoogleMap\Places\AutocompleteType; 
class PlaceCatAdmin extends Admin 
{ 
    public function configureListFields(ListMapper $listMapper) 
    { 
     $env = $this->container->get('myfunc')->getEnv(); 

如果你把你的ENV變量作爲公共那麼你也可以通過這種方式

$env = $this->container->get('myfunc')->env; 
+0

顯示此錯誤並且無法訪問容器...注意:未定義的屬性:Acme \ AdminBundle \ Admin \ PlaceCatAdmin :: $ container' – whitebear

+0

您需要在父類或當前類中擴展基本控制器,然後才能訪問類內的容器否則,你需要把你的類中的服務,並從那裏你可以把你的論點內部服務容器。 '使用Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; class PlaceCatAdmin extends Controller' or add this to you admin class 'use Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; class Admin extends Controller' –

+0

除非PlaceCatAdmin是一個控制器,否則讓它擴展基類控制器類的想法不僅很糟糕,而且它仍然不能解決注入容器的問題。嘗試一下。我知道它可能看起來像控制器神奇地訪問容器,但他們不知道。 handleRequest代碼實際上負責根據需要注入容器。 – Cerad