ZF3 - Call a function with parameters in factories' Module class

1.8k Views Asked by At

I'm new in ZendFramework3 and I just want to know if it's possible to call a function with more arguments than just the serviceManager in factories (within my Module class) : (Is it possible to pass an argument next to the $sm argument?)

//class Module

//getConfig()
//getServiceConfig()

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

There are 2 best solutions below

3
On

If you want to create one "magical" factory for all dependencies, you should take a look at SM AbstractFactory.

In that one factory you can check from the $requestedName what will be dependencies. For eg. you can read dependencies from class constructor and than inject that dependencies (or pull it again from $container). You can also set your dependencies in config, and in that way create only one factory for all your classes.

0
On

You may (for sure) pass other Arguments to Factory, so long as the Class to be created can handle the Arguments gracefully. In the example below, a Factory Class was created inside the Controller Directory. This is now the factory- ModuleControllerFactory - that instantiates the 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);
        }
    }

So, now we can create the Constructor of the ModuleController Class:

  <?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
            ]);
        }


    }

And now, update the module.config.php inside the config Folder of your module Example: 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
            ]
        ]
          //...

    ];