# Laminas-cli: service\_manager config in module.config.php doesn't work

**URL:** <https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263>\
**Category:** Components & MVC\
**Tags:** laminas-cli\
**Created:** [April 21, 2021, 8:07am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263 "2021-04-21T08:07:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 21, 2021, 8:07am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/1 "2021-04-21T08:07:49Z")

</div>

My problem is plain and simple: When trying to configure my cli module I seem to only be able to configure the service manager inside of my `src/Module.php` and not in my `config/module.config.php`.

This works:

```auto
class Module implements ConfigProviderInterface
{
    public function getConfig(): array
    {
        return include __DIR__. '/../config/module.config.php';
    }

    public function getServiceConfig(): array
    {
        return [
            'factories' => [
                HelloCommand::class => QueueCommandFactory::class,
                PublishCommand::class => QueueCommandFactory::class,
                RabbitmqService::class => InvokableFactory::class,
            ],
        ];
    }
}

```

But this doesn’t:

```auto
//config/module.config.php
return [
    'laminas-cli' => [
        'commands' => [
            'app:hello' => HelloCommand::class,
            'app:publish' => PublishCommand::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            HelloCommand::class => QueueCommandFactory::class,
            PublishCommand::class => QueueCommandFactory::class,
            RabbitmqService::class => InvokableFactory::class,
        ],
    ]
];

```

The error I get when I’m trying to run the command in the shell is:

```auto
Unable to resolve service "Worker\Command\PublishCommand" to a factory; are you certain you provided it during configuration?

```

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 21, 2021, 8:46am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/2 "2021-04-21T08:46:36Z")

</div>

Heyo,

Can you try dumping the merged config of your entire application, and see if it contains your section (and where)?

Usually `$container->get('config')`.

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 21, 2021, 9:58am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/3 "2021-04-21T09:58:20Z")

</div>

(Ignore the previous version of this post. I forgot to add the new module in modules.config.php. Here you go:)

The following is the result of creating a duplicate module - since my app only has one module - the original one “Worker” has the not working config in module.config.php and the new one “Tester” has the working config in the Module.php.

```auto
Array
(
    [service_manager] => Array
        (
            [aliases] => Array
                (
                    [HttpRouter] => Laminas\Router\Http\TreeRouteStack
                    [router] => Laminas\Router\RouteStackInterface
                    [Router] => Laminas\Router\RouteStackInterface
                    [RoutePluginManager] => Laminas\Router\RoutePluginManager
                    [Zend\Router\Http\TreeRouteStack] => Laminas\Router\Http\TreeRouteStack
                    [Zend\Router\RoutePluginManager] => Laminas\Router\RoutePluginManager
                    [Zend\Router\RouteStackInterface] => Laminas\Router\RouteStackInterface
                    [ValidatorManager] => Laminas\Validator\ValidatorPluginManager
                    [Zend\Validator\ValidatorPluginManager] => Laminas\Validator\ValidatorPluginManager
                )

            [factories] => Array
                (
                    [Laminas\Router\Http\TreeRouteStack] => Laminas\Router\Http\HttpRouterFactory
                    [Laminas\Router\RoutePluginManager] => Laminas\Router\RoutePluginManagerFactory
                    [Laminas\Router\RouteStackInterface] => Laminas\Router\RouterFactory
                    [Laminas\Validator\ValidatorPluginManager] => Laminas\Validator\ValidatorPluginManagerFactory
                    [Worker\Command\HelloCommand] => Worker\QueueCommandFactory
                    [Worker\Command\PublishCommand] => Worker\QueueCommandFactory
                    [Worker\RabbitmqService] => Worker\InvokableFactory
                )

        )

    [route_manager] => Array
        (
        )

    [router] => Array
        (
            [routes] => Array
                (
                )

        )

    [laminas-cli] => Array
        (
            [commands] => Array
                (
                    [app:hello] => Worker\Command\HelloCommand
                    [app:publish] => Worker\Command\PublishCommand
                    [test:hello] => Tester\Command\HelloCommand
                    [test:publish] => Tester\Command\PublishCommand
                )

        )

)

```

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 27, 2021, 7:25am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/4 "2021-04-27T07:25:55Z")

</div>

Anyone has an idea? Should I file a bug for this?

---

<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:** [April 27, 2021, 8:22am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/5 "2021-04-27T08:22:50Z")

</div>

> [@matti](#):
>
> the original one “Worker” has the not working config in module.config.php

Are sure? The factories and the commands of your `Worker` module are there:

> [@matti](#):
>
> ```auto
> [Worker\Command\HelloCommand] => Worker\QueueCommandFactory
> [Worker\Command\PublishCommand] => Worker\QueueCommandFactory
> [Worker\RabbitmqService] => Worker\InvokableFactory
> 
> ```
> 
> ```auto
> [app:hello] => Worker\Command\HelloCommand
> [app:publish] => Worker\Command\PublishCommand
> 
> ```

> [@matti](#):
>
> the new one “Tester” has the working config in the Module.php.

And the both commands of your `Tester` module are there:

> [@matti](#):
>
> ```auto
> [test:hello] => Tester\Command\HelloCommand
> [test:publish] => Tester\Command\PublishCommand
> 
> ```

If you have some factories in your `Tester` module then they are missing.

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 27, 2021, 10:24am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/6 "2021-04-27T10:24:35Z")

</div>

I get why you’d think that but as far as I can tell it is configured as I claim:

I have made the test-repo available [here](https://github.com/finn-matti/laminas-test). Maybe I’m doing something wrong?

---

<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:** [April 27, 2021, 10:41am UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/7 "2021-04-27T10:41:45Z")

</div>

Based on this example repo, some classes can not be found because of the wrong class names in the config. The namespaces are not included or wrong here.

First remove the method `Tester\Module::getServiceConfig()`.

Update the module configuration of `Worker`:

```php
namespace Worker;

use Laminas\ServiceManager\Factory\InvokableFactory;

return [
    'laminas-cli' => [
        'commands' => [
            'app:hello' => Command\HelloCommand::class,
            'app:publish' => Command\PublishCommand::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            Command\HelloCommand::class => Command\QueueCommandFactory::class,
            Command\PublishCommand::class => Command\QueueCommandFactory::class,
            Service\RabbitmqService::class => InvokableFactory::class,
        ],
    ],
];

```

Update the module configuration of `Tester`:

```php
namespace Tester;

use Laminas\ServiceManager\Factory\InvokableFactory;

return [
    'laminas-cli' => [
        'commands' => [
            'test:hello' => Command\HelloCommand::class,
            'test:publish' => Command\PublishCommand::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            Command\HelloCommand::class => Command\QueueCommandFactory::class,
            Command\PublishCommand::class => Command\QueueCommandFactory::class,
            Service\RabbitmqService::class => InvokableFactory::class,
        ],
    ],
];

```

Then I can run the commands with the service class `RabbitmqService` in module `Worker` and `Tester`. The service is created and called.

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 27, 2021, 1:28pm UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/8 "2021-04-27T13:28:43Z")

</div>

Thanks that solved it! So I guess you have to add the sub-namespaces to the commands and factories? But if I add a use statement before the return in the module.config.php to refer to the Command/Factory that I want to use that doesn’t work here? Do you have any idea why?

---

<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:** [April 27, 2021, 1:38pm UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/9 "2021-04-27T13:38:38Z")

</div>

> [@matti](#):
>
> But if I add a use statement before the return in the module.config.php to refer to the Command/Factory that I want to use that doesn’t work here?

It all works because it is normal PHP.

You can use one of these options and the result is the same:

```php
use Tester\Command\HelloCommand;
use Tester\Command\QueueCommandFactory;

return [
    'service_manager' => [
        'factories' => [
           HelloCommand::class =>QueueCommandFactory::class,
        ],
    ],
];

```

```php
namespace Tester;

return [
    'service_manager' => [
        'factories' => [
            Command\HelloCommand::class => Command\QueueCommandFactory::class,
        ],
    ],
];

```

```php
return [
    'service_manager' => [
        'factories' => [
            Tester\Command\HelloCommand::class => Tester\Command\QueueCommandFactory::class,
        ],
    ],
];

```

More on this topic can be found in the PHP documentation:

[https://www.php.net/manual/language.namespaces.basics.php](https://www.php.net/manual/language.namespaces.basics.php)

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 27, 2021, 1:52pm UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/10 "2021-04-27T13:52:17Z")

</div>

Thanks! I prefer the first version and was wondering why it didn’t work in my case. But I see now that I had missed some use statements for the factories and the rabbitmqservice. Oops. Thanks for taking a look.

---

<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:** [April 27, 2021, 1:57pm UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/11 "2021-04-27T13:57:31Z")

</div>

> [@matti](#):
>
> But I see now that I had missed some use statements for the factories and the rabbitmqservice.

Use an IDE like PHPStorm and any problems with namespaces and or class names will be shown immediately:

> **[Code Inspection: Undefined namespace | PhpStorm](https://www.jetbrains.com/help/phpstorm/php-undefined-namespace.html)**

> **[Auto import | PhpStorm](https://www.jetbrains.com/help/phpstorm/creating-and-optimizing-imports.html#completeShortClassName)**
>
> Basic procedures to create and optimize imports in PhpStorm. Learn more how to import the missing import or XML namespace.

---

<div class="post-metadata">

**Author:** ![matti](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matti/32/959_2.png) [@matti](https://discourse.laminas.dev/u/matti)\
**Post date:** [April 28, 2021, 3:48pm UTC](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/12 "2021-04-28T15:48:46Z")

</div>

Thanks! I use intelephense which normally works for autoimporting, but not if you copy/paste `MyClass:class` to a new location. 😉
