我试图为我的控制器实现工厂:
class NumberControllerFactory implements FactoryInterface{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new NumberController($container->get(Bar::class));
}
public function createService(ServiceLocatorInterface $services)
{
return $this($services, NumberController::class);
}}
我有错误:
Fatal error: Declaration of Number\Factory\NumberControllerFactory::__invoke() must be compatible with Zend\ServiceManager\Factory\FactoryInterface::__invoke(Interop\Container\ContainerInterface $container, $requestedName, array $options = NULL) in C:\xampp\htdocs\MyProject\module\Number\src\Number\Factory\NumberControllerFactory.php on line 10我需要这样做,因为我想将模型注入控制器,因为服务管理器已经从Zend 3中的控制器中删除了。
我使用了https://framework.zend.com/manual/2.4/en/ref/installation.html描述的骨架
在composer.json中是:
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
"zendframework/zend-mvc": "^3.0.1",
"zfcampus/zf-development-mode": "^3.0"
},我不明白这个问题,我读了很多教程,例如:
https://zendframework.github.io/zend-servicemanager/migration/
你能帮帮我吗?
我猜目前这种方法与Zend\ServiceManager\Factory\FactoryInterface::__invoke兼容。
发布于 2016-11-25 18:05:23
为了将模型注入控制器,您需要在module.config.php中进行配置时创建一个工厂类,如下所示
'controllers' => [
'factories' => [
Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
],
],这里,AlbumController是相册模块的控制器类。之后,您需要在模块\ AlbumControllerFactory \src\Factory中创建一个类。在这个类中,您需要编写以下代码:
namespace Album\Factory;
use Album\Controller\AlbumController;
use Album\Model\AlbumTable;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class AlbumControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new AlbumController($container->get(AlbumTable::class));
}
}您需要在控制器类(AlbumController)中编写以下代码。
public function __construct(AlbumTable $album) {
$this->table = $album;
}这样,您就可以将模型类注入控制器类。
发布于 2016-11-26 12:02:43
谢谢阿兹哈尔。
我的问题是当我用:
'factories' => array(
\Number\Controller\NumberController::class => \Number\Factory\NumberControllerFactory::class
)不是工作,是404.我不得不用:
'Number\Controller\Number' => \Number\Factory\NumberControllerFactory::class在文档中,我应该使用完整的类名::class。
有人知道为什么不起作用吗?
https://stackoverflow.com/questions/40809585
复制相似问题