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



Symfony 2.7应用程序中有以下控制器:

namespace MyCompanyAppBundleController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
class PdfPrevewController extends Controller
{
public function __construct()
{
//TODO: Inject the em instead of extending controller.
//      (An error resulted when attempting to do that.)
$this->em = $this->get('doctrine.orm.default_entity_manager');
}
/**
* @Route("/admin/pdf-preview/by-document-id/{id}", name="pdf_preview_by_document_id")
*/
public function createPdfPreviewAction($id = 0)
{
die('Started.');
}
}

当我在浏览器中打开控制器时,我会收到以下消息:

错误:在空上调用成员函数get((

。。。我真的不明白,因为扩展控制器类通常可以访问容器。我在这里错过了什么?

===

更新:我还尝试将我的控制器定义为服务,并在那里设置容器:

app.controller.pdf_preview:
class: ExozetAppBundleControllerPdfPreviewController
calls:
- [setContainer, ['@service_container']]

运气不佳。仍然显示相同的错误消息。

$this->em = $this->get('doctrine.orm.default_entity_manager');

在构造函数中还不可用。。。相反,使用依赖项注入来设置entitymanager(干净方法(

public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}

或者稍后调用另一个功能中的服务(当所有服务都已建立时(

最新更新