Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对null上的成员函数get()的Symfony调用_Symfony - Fatal编程技术网

对null上的成员函数get()的Symfony调用

对null上的成员函数get()的Symfony调用,symfony,Symfony,我在自己的框架中使用Symfony组件。我安装了DI组件并试图在BaseController中获取它,但每次都出现相同的错误: 在null上调用成员函数get() 我的基地控制器: abstract class BaseController implements ContainerAwareInterface { use ContainerAwareTrait; ... } 当我试图访问$this->container->get('ser_id')返回对成员函数的调用 如何解决

我在自己的框架中使用Symfony组件。我安装了DI组件并试图在BaseController中获取它,但每次都出现相同的错误:

在null上调用成员函数get()

我的基地控制器:

abstract class BaseController implements ContainerAwareInterface
{
    use ContainerAwareTrait;
    ...
}
当我试图访问
$this->container->get('ser_id')返回对成员函数的调用

如何解决这个问题

更新:

只有在控制器服务容器中包含时才能工作

$this->service_container = include __DIR__ .'/../../../container.php';
然后像这样使用它:

$this->service_container->get('id');

无论创建什么代码,控制器都需要对其调用setContainer。Symfony框架控制器解析器可以为您执行此操作。您需要相应地调整代码。您需要扩展ContainerWare类,而不是实现ContainerWareInterface。Jasmin Mistry
ContainerWare
类不存在。该特性负责实现。创建控制器时只需调用setContainer。好的,这不是问题。但是,
setContainer
需要返回什么。在内部传递什么?无论创建什么代码,控制器都需要在其上调用setContainer。Symfony框架控制器解析器可以为您执行此操作。您需要相应地调整代码。您需要扩展ContainerWare类,而不是实现ContainerWareInterface。Jasmin Mistry
ContainerWare
类不存在。该特性负责实现。创建控制器时只需调用setContainer。好的,这不是问题。但是,
setContainer
需要返回什么。在里面传递什么?