我现在的结构是有一个CommonController 然后在里面写了查询菜单的方法,子类继承的时候,还必须得去调用这个方法才行。
能不能在构造函数里面使用 $this->doctrine()->getManager() 这个函数。 我在构造函数里写就报错,找不到has()。
请问该如何做才可以,谢谢~
Parent.php
class AParent{
private $menu;
public function __construct(){
$this->menu = $this->doctrine()->getManager()
->getRepository(Menu')->findAll();
}
}
Child.php
class Child extends AParent{
public function __construct(){
parent::__construct();
var_dump(parent::$menu);
}
}
这样是不行的 你要使用 Dependency Injection:
你的
可以改为:
在 controller中使用 :
你还可以设置成servive:
services.yml:
然后在controller中使用 :