在简单的包 class 中使用容器在 Symfony 2 中不起作用
Using the container inside a simple bundle class dont work in Symfony 2
我的错误:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to
Agc\ManagerBundle\Lib\grafica::__construct() must implement interface
Symfony\Component\DependencyInjection\ContainerInterface, none given,
called in
C:\wamp\www\galileo\src\Agc\BackendBundle\Controller\DefaultController.php
on line 20 and defined in
C:\wamp\www\galileo\src\Agc\ManagerBundle\Lib\grafica.php line 10
我的 class 图片:
<?php
namespace Agc\ManagerBundle\Lib;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @Route(service="srv_grafica")
*/
class grafica
{
private $container, $conn, $bdnmgi;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->conn = $this->getConn();
$this->bdnmgi = $this->getBd();
}
public function getConn(){
return $this->container->get('database_connection');
}
public function getBd(){
return $this->container->get('security.context')->getToken()->getUser()->getAdministracion()->getNombreEsquemamgi();
}
\ManagerBundle\Resources\config\services.yml
services:
srv_grafica:
class: Agc\ManagerBundle\Lib\grafica
arguments:
- '@service_container'
我的默认控制器:
class DefaultController extends Controller
{
public function dashboardAction(Request $peticion)
{
$em = $this->getDoctrine()->getManager('customer_1');
$user= $this->get('security.context')->getToken()->getUser();
$esquema = $user->getAdministracion()->getNombreEsquemamgi();
var_dump($esquema);
$grafica = new grafica();
您需要将容器传递给您的服务,下面是如何在 services.yml
中执行此操作的示例
services:
srv_grafica:
class: Agc\ManagerBundle\Lib\grafica
arguments:
- '@service_container'
错了:
$grafica = new grafica();
应该是:
$grafica = $this->get('srv_grafica');
我的错误:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Agc\ManagerBundle\Lib\grafica::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in C:\wamp\www\galileo\src\Agc\BackendBundle\Controller\DefaultController.php on line 20 and defined in C:\wamp\www\galileo\src\Agc\ManagerBundle\Lib\grafica.php line 10
我的 class 图片:
<?php
namespace Agc\ManagerBundle\Lib;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @Route(service="srv_grafica")
*/
class grafica
{
private $container, $conn, $bdnmgi;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->conn = $this->getConn();
$this->bdnmgi = $this->getBd();
}
public function getConn(){
return $this->container->get('database_connection');
}
public function getBd(){
return $this->container->get('security.context')->getToken()->getUser()->getAdministracion()->getNombreEsquemamgi();
}
\ManagerBundle\Resources\config\services.yml
services:
srv_grafica:
class: Agc\ManagerBundle\Lib\grafica
arguments:
- '@service_container'
我的默认控制器:
class DefaultController extends Controller
{
public function dashboardAction(Request $peticion)
{
$em = $this->getDoctrine()->getManager('customer_1');
$user= $this->get('security.context')->getToken()->getUser();
$esquema = $user->getAdministracion()->getNombreEsquemamgi();
var_dump($esquema);
$grafica = new grafica();
您需要将容器传递给您的服务,下面是如何在 services.yml
中执行此操作的示例services:
srv_grafica:
class: Agc\ManagerBundle\Lib\grafica
arguments:
- '@service_container'
错了:
$grafica = new grafica();
应该是:
$grafica = $this->get('srv_grafica');