0
votes

From my Factory i am passsing serviceLocator to the controller, but it seems like ObservationFactory is not being called even i have included in the module.config.php file of the Module

Here is my ObservationController.php

    class ObservationController extends AbstractActionController
{
    private $em;
    public function __construct($ob)
    {
        $this->em = $ob->get('doctrine.entitymanager.orm_default');
    }

here is ObservationFactory.php

    class ObservationFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $controller = new ObservationController($serviceLocator->getServicelocator());
        return $controller;
    }
}

and in Module.config.php

'controllers' => array(
    'factories' => array(
        'Observation\Controller\ObservationController' => 'Observation\Controller\ObservationFactory'
    ),
    'invokables' => array(
            'Observation\Controller\Observation'=> Controller\ObservationController::class
    ),
),

Missing argument 1 for Observation\Controller\ObservationController::__construct(), called in S:\xampp\htdocs\v3\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php on line 207 and defined in S:\xampp\htdocs\v3\module\Observation\src\Observation\Controller\ObservationController.php on line 47

Notice: Undefined variable: ob in S:\xampp\htdocs\v3\module\Observation\src\Observation\Controller\ObservationController.php on line 49

Fatal error: Uncaught Error: Call to a member function get() on unknown in

2
What happens if you change your controller factories entry into ObervationController::class => ObervationControllerFactory::class? Maybe your invokables trouble your factory. What happens if you leave out the invokables section in your controller array?Marcel
Yes that invokables was the issue, removed it and now the factory worksWaqar Haider
Since you are already injecting the ServiceLocatorInterface into your controller just to grab the entitymanager, why wont you use proper dependency injection and just inject the entitymanager into your controller instead?boesing

2 Answers

2
votes

As your ObservationController is taking an argument in its constructor, you do not need to call your controller as invokables any more. It would not work. Because an invokable class can not be constructed with arguments.

Whereas whenever you need argument(s) for your controller, you should make a factory for that controller what you already did. Just hide or remove the ObservationController under the invokables key as the following

'controllers' => array(
    'factories' => array(
        'Observation\Controller\Observation' => 'Observation\Controller\ObservationFactory'
    ),
    'invokables' => array(
        // 'Observation\Controller\Observation'=> Controller\ObservationController::class
    ),
),   
2
votes

If you want to prepare your code for ZF3, use this structure :

return [
    'controllers' => [
        'aliases' => [
            'Observation\Controller\Observation' => Observation\Controller\ObservationController::class,
        ],
        'factories' => [
            Observation\Controller\ObservationController::class => Observation\Controller\ObservationControllerFactory::class,
        ],
    ],
];