官方教程是:在控制器中的构造函数中写上需要访问的Model参数。
现在有一个控制器:CommentController ,构造函数如下:
private $commentService;
private $questionService;
public function __construct(CommentInterface $commentService,QuestionInterface $questionService)
{
$this->commentService = $commentService;
$this->questionService = $questionService;
}
然后工厂类 CommentControllerFactory 里面实例化:
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new CommentController($container->get(CommentInterface::class),$container->get(QuestionInterface::class));
}
Model 如下
use Zend\Db\Adapter\AdapterInterface;
class Comment implements CommentInterface
{
private $db;
public function __construct(AdapterInterface $db)
{
$this->db = $db;
}
...
}
现在我想直接在控制器里面访问Test Model
class Comment {
...
public function testAction(){
$this->testService = new Test();
}
}
Test Model 如下:
use Zend\Db\Adapter\AdapterInterface;
class Test
{
private $db;
public function __construct(AdapterInterface $db)
{
$this->db = $db;
}
}
请问应该如何实现?还是说框架机制不允许,在官网上没有找到答案