我在自己的框架中使用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');
1条答案
按热度按时间hjzp0vay1#
好吧,一个有点老的问题,但也许有人会坚持这个问题。在实现
ContainerAwareInterface
和use ContainerAwareTrait
之后,您需要调用setContainer。示例: