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

xdnvmnnf  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(100)

我在自己的框架中使用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');
hjzp0vay

hjzp0vay1#

好吧,一个有点老的问题,但也许有人会坚持这个问题。在实现ContainerAwareInterfaceuse ContainerAwareTrait之后,您需要调用setContainer。示例:

// Controller or Service class
class SomeClass implements ContainerAwareInterface {
   use ContainerAwareTrait;

   //some logic
   //..........
}

// services.yml 
your_controller:
    class: SomeClass
    calls:
        - [ setContainer, [ '@service_container' ] ]

相关问题