# Service manager does not care if there's a factory or not

**URL:** <https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473>\
**Category:** Components & MVC\
**Tags:** laminas-servicemanager\
**Created:** [September 19, 2023, 10:34pm UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473 "2023-09-19T22:34:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![malwon](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/malwon/32/438_2.png) [@malwon](https://discourse.laminas.dev/u/malwon)\
**Post date:** [September 19, 2023, 10:34pm UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/1 "2023-09-19T22:34:06Z")

</div>

Hi!

It seems, that the service-manager doesn’t care anymore, if a service was initiated or not.

Example of my Module.php:

```php

    public function getControllerConfig()
    {
        return [
            'factories' => [
                Brand\Controller\JsonDataController::class => function ($container) {
                    $ctrl = new Brand\Controller\JsonDataController();
                    $ctrl->setBrandTable($container->get(BrandTable::class));
                    return $ctrl;
                },
         ];
     }

```

But `BrandTable` was never initiated. Doesn’t this trigger an error, sth. like “Service not found. Are you sure you mapped it to factory?”

I’m confused.

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [September 20, 2023, 10:17am UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/2 "2023-09-20T10:17:06Z")

</div>

Hey Malwon,

in this scenario `BrandTable::class` results into a string of the fully qualified classname. It is not an instance of the `BrandTable` object. The service manager may result into null in your case. This indicates, that you have registered `BrandTable::class` somewhere in your config already. You could solve this by type hinting the controller constructor method like the following example:

```auto
<?php

declare(strict_types=1);

namespace Marcel\Controller;

class MyController
{
    public function __construct(
        protected BrandTable $table
    ) {}

    ...
}

```

In this case the controller awaits an instance of `BrandTable`. If this is not given it will result into an error in any case.

---

<div class="post-metadata">

**Author:** ![malwon](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/malwon/32/438_2.png) [@malwon](https://discourse.laminas.dev/u/malwon)\
**Post date:** [September 22, 2023, 8:50pm UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/3 "2023-09-22T20:50:26Z")

</div>

Hi ezkimo,

thanks for caring to answer.

> You could solve this by type hinting the controller constructor method like the following example

I actually did type hinting.

> This indicates, that you have registered `BrandTable::class` somewhere in your config already.

No, I didn’t.

**I jut now tried the following to demonstrate what I mean:**

This is my folder structure:  
 ![Screenshot_20230922_223320](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/5/5418bbe33bfee2fdaca4ee24d5213cf64e524f72.png)

My Module.php:

```php
public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\IndexController::class => function ($container) {
                    $ctrl = new Controller\IndexController;
                    $ctrl->setFoo($container->get(Model\Foo::class));
                    return $ctrl;
                }
            ]
        ];
    }

```

My Controller:

```php
class IndexController extends AbstractActionController
{

    protected Foo $foo;

    public function indexAction()
    {
        return new ViewModel();
    }

    /**
     * Set the value of foo
     *
     * @param Foo $foo
     *
     * @return self
     */
    public function setFoo(Foo $foo): self
    {
        $this->foo = $foo;

        return $this;
    }
}

```

This works! But actually it shouldn’t, cause I never provided a factory for my `Foo`-model.

Why **doesn’t** it throw this usual error:

> Unable to resolve service “Application\Model\Foo” to a factory; are you certain you provided it during configuration?

🤔

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [September 24, 2023, 7:20pm UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/4 "2023-09-24T19:20:26Z")

</div>

Please check if an abstract factory is registered in your application which handles the creation of the class `Foo`. Search for `abstract_factories` in your code.

> **[Configuring the service manager - laminas-servicemanager - Laminas Docs](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/#abstract-factories)**
>
> laminas-servicemanager: factory-driven dependency injection container

> **[Factories vs Abstract Factories - laminas-servicemanager - Laminas Docs](https://docs.laminas.dev/laminas-servicemanager/cookbook/factories-vs-abstract-factories/)**
>
> laminas-servicemanager: factory-driven dependency injection container

---

<div class="post-metadata">

**Author:** ![malwon](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/malwon/32/438_2.png) [@malwon](https://discourse.laminas.dev/u/malwon)\
**Post date:** [September 25, 2023, 1:22am UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/6 "2023-09-25T01:22:34Z")

</div>

@Tyrsson I don’t know why you deleted your post, because it’s the solution! Fortunately, your original reply is still readable. 😄 For some reason, I just must have pressed “yes” when the laminas-mvc installation-routine asked me about Laminas\Di.

Removing ` 'Laminas\Di'` from `config/modules.config.php` helped. And now the error:

> Unable to resolve service “Application\Model\Foo” to a factory; are you certain you provided it during configuration?

is thrown, as it is supposed to be, isn’t it?

Thank you man, and all others that posted their suggestions.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [September 25, 2023, 8:21am UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/7 "2023-09-25T08:21:26Z")

</div>

> [@malwon](#):
>
> Removing ` 'Laminas\Di'` from `config/modules.config.php` helped. And now the error:

laminas-di comes with a registered abstract factory:

> <https://github.com/laminas/laminas-di/blob/8d4074b5f51b747a6e4e055995f1bdf2a825d5a6/src/ConfigProvider.php#L49-L51>

> **[Usage with laminas-servicemanager - laminas-di - Laminas Docs](https://docs.laminas.dev/laminas-di/v3/cookbook/use-with-servicemanager/#abstractgeneric-service-factory)**
>
> Automated dependency injection for PSR-11 containers

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [September 26, 2023, 8:06am UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/8 "2023-09-26T08:06:10Z")

</div>

@malwon, This error-solving technique has really made someone a real deep-thinking developer. Now, someone can build an entire framework on his own or even a module in Laminas MVC. This is really helpful. Thanks!

---

<div class="post-metadata">

**Author:** ![Tyrsson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tyrsson/32/1664_2.png) [@Tyrsson](https://discourse.laminas.dev/u/Tyrsson)\
**Post date:** [September 30, 2023, 9:21pm UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/9 "2023-09-30T21:21:10Z")

</div>

Can’t remember why I removed it…

Lamians-Di has caused many issues like this. It caused me a major issue using Laminas-Authentication since it autowired an instance when laminas-auth does not provide a factory. Honestly, in my opinion it should be removed from the list of dependencies that is offered in the create-project routine.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 4, 2023, 8:18am UTC](https://discourse.laminas.dev/t/service-manager-does-not-care-if-theres-a-factory-or-not/3473/10 "2023-10-04T08:18:34Z")

</div>

> [@Tyrsson](#):
>
> …in my opinion it should be removed from the list of dependencies that is offered in the create-project routine.

Please create an issue report or a pull request for it, otherwise your opinion will be lost.

> <https://github.com/laminas/laminas-mvc-skeleton/blob/e4bfbffdd5c52b680f515fa4853efefd0bc6d7fa/composer.json#L96-L101>
