Php Zend 3工厂接口
我正在尝试为我的控制器实现工厂:Php Zend 3工厂接口,php,zend-framework,dependencies,factory,zend-framework3,Php,Zend Framework,Dependencies,Factory,Zend Framework3,我正在尝试为我的控制器实现工厂: class NumberControllerFactory implements FactoryInterface{ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { return new NumberController($container->get(Bar::class)); } public fu
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中,服务管理器已经从控制器中删除
我使用了中描述的骨架
在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"
},
我不理解这个问题,我读了很多教程,例如:
你能帮帮我吗
我猜当前此方法与Zend\ServiceManager\Factory\FactoryInterface兼容::\uu invoke要将模型注入控制器,您需要在module.config.php中进行配置时创建一个Factory类,如下所示
'controllers' => [
'factories' => [
Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
],
],
此处AlbumController是Album模块的控制器类。之后,需要在module\Album\src\Factory内创建AlbumControllerFactory类。
在本课程中,您需要编写以下代码:
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)中编写以下代码
通过这种方式,您可以将模型类注入控制器类。谢谢Azhar
我的问题是因为当我使用:
'factories' => array(
\Number\Controller\NumberController::class => \Number\Factory\NumberControllerFactory::class
)
那没用,有404。。。我必须使用:
'Number\Controller\Number' => \Number\Factory\NumberControllerFactory::class
在文档中,我应该使用完整的类名::class
有人知道它为什么不工作吗?您可以使用belwo代码“Number\Controller\Number”=>Factory\NumberControllerFactory::class或Controller\NumberController::class=>Factory\NumberControllerFactory::class
'Number\Controller\Number' => \Number\Factory\NumberControllerFactory::class