# Moving getControllerConfig to module.config.php

**URL:** <https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343>\
**Category:** Components & MVC\
**Tags:** laminas-mvc, laminas-servicemanager\
**Created:** [June 10, 2021, 9:33am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343 "2021-06-10T09:33:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 9:33am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/1 "2021-06-10T09:33:07Z")

</div>

Hi everyone!

I’ve been trying to get rid of the getControllerConfig() Method, and move it over to the Module’s module.config.php.  
Sadly, in the progress of doing so, I am encountering this error:

```auto
A 404 error occurred
Page not found.

The requested controller could not be mapped to an existing controller class.

Controller:
    Project\Controller\ProjectController (resolves to invalid controller class or alias: Project\Controller\ProjectController) 

No Exception available

```

My Module.php:

```auto
<?php

namespace Project;

use Laminas\ModuleManager\Feature\ConfigProviderInterface;
use Project\Controller\Factory\ProjectControllerFactory;
use Project\Model\Factory\ProjectTableFactory;
use Project\Model\Factory\ProjectTableGatewayFactory;

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

    // public function getControllerConfig()
    // {
    // return [
    // 'factories' => [
    // Controller\ProjectController::class => ProjectControllerFactory::class
    // ],
    // ];
    // }
}

```

And my module.config.php:

```auto
<?php

namespace Project;

use Laminas\Router\Http\Segment;
use Project\Controller\Factory\ProjectControllerFactory;
use Project\Controller\ProjectController;
use Project\Model\Factory\ProjectTableFactory;
use Project\Model\Factory\ProjectTableGatewayFactory;
use Project\Model\ProjectTable;

return [
    'router' => [
        'routes' => [
            'project' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/project[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Project\Controller\ProjectController::class,
                        'action' => 'index',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'project' => __DIR__. '/../view',
        ],
    ],
    'service_manager' => [
        'factories' => [
            ProjectTable::class => ProjectTableFactory::class,
            Model\ProjectTableGateway::class => ProjectTableGatewayFactory::class,
        ]
    ],
    'controller' => [
        'factories' => [
            ProjectController::class => ProjectControllerFactory::class
        ]
    ]
];

```

Your help would be greatly appreciated.

Also, [this post](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/12) helped me move the getServiceConfig() over. I just missed some import statements.

---

<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:** [June 10, 2021, 9:37am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/2 "2021-06-10T09:37:43Z")

</div>

Hello and welcome to our forums! 😃

> [@makxk](#):
>
> I just missed some import statements.

The usage of namespace and classnames are wrong. You set the namespace to `Project` and then you use for the controller `Project\Controller\ProjectController::class`.  
The result is: `Project\Project\Controller\ProjectController::class`

Update this line:

```php
'controller' => Controller\ProjectController::class,

```

See also the example 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:** ![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:** [June 10, 2021, 9:44am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/3 "2021-06-10T09:44:01Z")

</div>

Ah, there is also an import for the controller class:

> [@makxk](#):
>
> `use Project\Controller\ProjectController;`

So you can write:

```php
'controller' => ProjectController::class,

```

See also: [Laminas-cli: service\_manager config in module.config.php doesn't work - #9 by froschdesign](https://discourse.laminas.dev/t/laminas-cli-service-manager-config-in-module-config-php-doesnt-work/2263/9)

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 9:45am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/4 "2021-06-10T09:45:25Z")

</div>

You’re fast, thanks!  
That line is under defaults beneath router, right?  
Error is still there. (For good measure, I composer dump’ed autoload and restarted the php dev server)  
Updated file:

```auto
<?php

namespace Project;

use Laminas\Router\Http\Segment;
use Project\Controller\Factory\ProjectControllerFactory;
use Project\Controller\ProjectController;
use Project\Model\Factory\ProjectTableFactory;
use Project\Model\Factory\ProjectTableGatewayFactory;
use Project\Model\ProjectTable;

return [
    'router' => [
        'routes' => [
            'project' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/project[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => ProjectController::class, //changed this
                        'action' => 'index',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'project' => __DIR__. '/../view',
        ],
    ],
    'service_manager' => [
        'factories' => [
            ProjectTable::class => ProjectTableFactory::class,
            Model\ProjectTableGateway::class => ProjectTableGatewayFactory::class,
        ]
    ],
    'controller' => [
        'factories' => [
            ProjectController::class => ProjectControllerFactory::class
        ]
    ]
];

```

---

<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:** [June 10, 2021, 9:53am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/5 "2021-06-10T09:53:34Z")

</div>

> [@makxk](#):
>
> Error is still there.

The config file looks good now. Please check the Composer configuration and the filenames.

If you use the skeleton application then the Composer configuration should contain:

```json
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Project\\": "module/Project/src/"
    }
},

```

And the filenames must be:

- `module/Project/src/Controller/ProjectController.php`
- `module/Project/src/Controller/Factory/ProjectControllerFactory.php`

Check also the namespace and classname in the files:

```php
namespace Project\Controller;

class ProjectController

```

```php
namespace Project\Controller\Factory;

class ProjectControllerFactory

```

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 10:50am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/6 "2021-06-10T10:50:39Z")

</div>

The composer config, the paths, the filenames, the namespaces and the class names are looking good so far.  
module\Project\src\Controller\ProjectController.php:

```auto
<?php

namespace Project\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Project\Model\ProjectTable;
use Project\Form\ProjectForm;
use Project\Model\Project;
use Laminas\Json\Encoder;
use InvalidArgumentException;
use Project\Form\UploadForm;
use Project\Form\Validator\DirectoriesCreated;

class ProjectController extends AbstractActionController
{
    private $table;

```

module\Project\src\Controller\Factory\ProjectControllerFactory.php:

```auto
<?php
namespace Project\Controller\Factory;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Project\Controller\ProjectController;
use Project\Model\ProjectTable;

class ProjectControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {

```

---

<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:** [June 10, 2021, 10:53am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/7 "2021-06-10T10:53:00Z")

</div>

This is wrong:

> [@makxk](#):
>
> ```auto
> 'controller' => [
> 'factories' => [
> ProjectController::class => ProjectControllerFactory::class
> ]
> ]
> 
> ```

It must be `controllers`! 🙈

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 10:53am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/8 "2021-06-10T10:53:58Z")

</div>

Jackpot lol, thanks again!

Dayum, feeling stupid now.

---

<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:** [June 10, 2021, 11:02am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/9 "2021-06-10T11:02:27Z")

</div>

Please keep in mind that there was also a problem with the namespace.

> [@makxk](#):
>
> Dayum, feeling stupid now.

No, the problem is when a configuration file type is used that does not support a schema.

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 11:06am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/10 "2021-06-10T11:06:33Z")

</div>

Some sort of schematic config would indeed be pretty cool. I saw there’s [laminas-config](https://docs.laminas.dev/laminas-config/), could that be used to get that?

---

<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:** [June 10, 2021, 11:39am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/11 "2021-06-10T11:39:07Z")

</div>

> [@makxk](#):
>
> Some sort of schematic config would indeed be pretty cool.

There is already an industry standard for it: XML 😃

> [@makxk](#):
>
> I saw there’s [laminas-config](https://docs.laminas.dev/laminas-config/), could that be used to get that?

No, that does not work. The config object uses internal an array as storage and the magic methods for setter and getter. Without special comments it does not help.

There are some attempts to solve this via extensions of IDEs, like:

> **[GitHub - klesun/deep-assoc-completion: A phpstorm plugin for associative...](https://github.com/klesun/deep-assoc-completion)**
>
> A phpstorm plugin for associative array key typing and completion - GitHub - klesun/deep-assoc-completion: A phpstorm plugin for associative array key typing and completion

> **[GitHub - klesun/deep-assoc-completion-vscode: A work-in-progress extension...](https://github.com/klesun/deep-assoc-completion-vscode)**
>
> A work-in-progress extension for vscode similar to deep-assoc-completion for phpstorm - GitHub - klesun/deep-assoc-completion-vscode: A work-in-progress extension for vscode similar to deep-assoc-c...

This works for PHP arrays but at the moment it does not help with schemas from multiple classes or comments.  
And in the end, it’s also only a workaround.

* * *

I would not make this dependent on a framework or a special tool like an extension, it should be available everywhere.

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 10, 2021, 11:56am UTC](https://discourse.laminas.dev/t/moving-getcontrollerconfig-to-module-config-php/2343/12 "2021-06-10T11:56:54Z")

</div>

> [@froschdesign](#):
>
> > [@makxk](#):
> >
> > Some sort of schematic config would indeed be pretty cool.
> 
> There is already an industry standard for it: XML 😃

isn’t json more common? 😜 😉

> [@froschdesign](#):
>
> > [@makxk](#):
> >
> > I saw there’s [laminas-config](https://docs.laminas.dev/laminas-config/), could that be used to get that?
> 
> No, that does not work. The config object uses internal an array as storage and the magic methods for setter and getter. Without special comments it does not help.
> 
> There are some attempts to solve this via extensions of IDEs, like:
> 
> [GitHub - klesun/deep-assoc-completion: A phpstorm plugin for associative array key typing and completion](https://github.com/klesun/deep-assoc-completion)
> 
> [GitHub - klesun/deep-assoc-completion-vscode: A work-in-progress extension for vscode similar to deep-assoc-completion for phpstorm](https://github.com/klesun/deep-assoc-completion-vscode)
> 
> This works for PHP arrays but at the moment it does not help with schemas from multiple classes or comments.  
> And in the end, it’s also only a workaround.
> 
> * * *
> 
> I would not make this dependent on a framework or a special tool like an extension, it should be available everywhere.

Wow, these plugin look awesome. I might be giving them a try somewhen.  
In the end, you’re right again; there should be some sort of universally available solution. If a language server implemented that, that’d get pretty close to an independent tool. Maybe some folks end up adapting one of the deep assoc plugins for the IDEs.
