1

我是 ZendFramework3 的新手,我只是想知道是否可以调用具有更多参数的函数,而不仅仅是工厂中的 serviceManager(在我的 Module 类中):(是否可以在参数旁边传递$sm参数?)

//class Module

//getConfig()
//getServiceConfig()

public function getControllerConfig()
{
    return [
        'factories' => [
            Controller\ModuleController::class => function ($sm) {
                return new ModuleController($sm);
            }
        ]
    ];
}
4

2 回答 2

1

如果你想为所有依赖创建一个“神奇”的工厂,你应该看看SM AbstractFactory

在那个工厂中,您可以从$requestedName检查什么是依赖项。例如。您可以从类构造函数中读取依赖项,然后注入该依赖项(或再次从 $container 中提取)。您还可以在 config 中设置依赖项,这样就可以为所有类只创建一个工厂。

于 2016-10-23T19:34:01.360 回答
0

您可以(当然)将其他参数传递给工厂,只要要创建的类可以优雅地处理参数。在下面的示例中,在控制器目录中创建了一个工厂类。这就是工厂——ModuleControllerFactory——实例化了ModuleController。

<?php

    namespace Application\Controller;

    use Application\Controller\ModuleController,
        Zend\ServiceManager\Factory\FactoryInterface,
        Zend\ServiceManager\Factory,
        Interop\Container\ContainerInterface;

    class ModuleControllerFactory implements FactoryInterface {

        public function __invoke(ContainerInterface $container, $requestedName, array $options=null){
            // WE WANT TO PASS $variable1 to $variable12 TO THE ModuleController
            $variable1      = "Variable Value 1";
            $variable2      = "Variable Value 2";
            $variable3      = "Variable Value 3";
            return new ModuleController($container, $variable1, $variable2, $variable3);
        }
    }

所以,现在我们可以创建 ModuleController 类的构造函数:

  <?php

    // FILE-NAME: ModuleController.php;
    namespace Application\Controller;
    use Interop\Container\ContainerInterface;

    class ModuleController extends AbstractActionController {
        /**
         * ContainerInterface.
         * @var ContainerInterface
         */
        public $container;

        /**
         * @var string
         */
        public $var1;

        /**
         * @var string
         */
        public $var2;

        /**
         * @var string
         */
        public $var3;

        //  CONTAINER & VARIALBLES INJECTED IN CONSTRUCTOR
        public function __construct(ContainerInterface $container, $variable1=null, $variable2=null, $variable3=null) {
            $this->container    = $container;
            $this->var1         = $variable1;
            $this->var2         = $variable2;
            $this->var3         = $variable3;
        }

        // JUST PLAY WITH THE INJECTED VALUES IN THE INDEX ACTION
        public function indexAction() {
            return new ViewModel([
                'container' => $this->container,
                'var1'      => $this->var1,
                'var2'      => $this->var2,
                'var3'      => $this->var3
            ]);
        }


    }

现在,更新module.config.php模块示例的配置文件夹内部:module/Application/config/module.config.php.

<?php

    namespace Application;

    use Zend\Router\Http\Literal;
    use Zend\Router\Http\Regex;
    use Zend\Router\Http\Segment;
    use Zend\ServiceManager\Factory\InvokableFactory;

    return [
          //...
        'router' => [
          //...
        ],
        'controllers' => [
            'factories' => [
                Controller\ModuleController::class => Controller\ModuleControllerFactory::class
            ]
        ]
          //...

    ];
于 2016-10-23T16:27:50.060 回答