# Best Practice: Location of Configuration

**URL:** <https://discourse.laminas.dev/t/best-practice-location-of-configuration/3703>\
**Category:** Components & MVC\
**Tags:** laminas-mvc\
**Created:** [May 24, 2024, 6:41pm UTC](https://discourse.laminas.dev/t/best-practice-location-of-configuration/3703 "2024-05-24T18:41:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![COMCDUARTE](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/comcduarte/32/1005_2.png) [@COMCDUARTE](https://discourse.laminas.dev/u/COMCDUARTE)\
**Post date:** [May 24, 2024, 6:41pm UTC](https://discourse.laminas.dev/t/best-practice-location-of-configuration/3703/1 "2024-05-24T18:41:41Z")

</div>

I’m creating a module with some custom HTML Input Form Elements. I thought it best to replicate the structure and format of laminas-form, but I’m finding there are multiple ways to add config to a module. My question is, is there a standard or best practice to follow? For the different methods which I’ll illustrate below, are there benefits to one over another, or is it simply the author’s preference?

In addition to providing new custom HTML elements, I would also like to have the ability to overwrite the existing HTML elements provided by laminasform by simply adding the module.

_Poll ([view on site](https://discourse.laminas.dev/t/best-practice-location-of-configuration/3703/1))_

## Array in module.config.php

_module.config.php_

```auto
<?php
declare(strict_types=1);

namespace ModuleName;

return [
    'form_elements' => [
        'aliases' => [
        ]
        'factories' => [
        ],
    ],
    'view_helpers' => [
        'aliases' => [
        ],
        'factories' => [
        ],
    ],
];

```

_Module.php_

```auto
<?php
declare(strict_types=1);

namespace ModuleName;

class Module
{
    public function getConfig(): array
    {
        /** @var array $config */
        $config = include __DIR__. '/../config/module.config.php';
        return $config;
    }
}

```

## In ConfigProvider.php

_Module.php_

```auto
<?php
declare(strict_types=1);

namespace ModuleName;

class ConfigProvider implements ViewHelperProviderInterface,FormElementProviderInterface
{
    public function __invoke(): array
    {
        return [
            'view_helpers' => $this->getViewHelperConfig(),
            'form_elements' => $this->getFormElementConfig(),
        ];
    }
    
    public function getViewHelperConfig(): array
    {
        return [
            'aliases' => [
            ],
            'factories' => [
            ],
        ];
    }
    
    public function getFormElementConfig()
    {
        return [
            'aliases' => [
            ],
            'factories' => [
            ],
        ];
    }

```

_Module.php_

```auto
<?php
declare(strict_types=1);

namespace ModuleName;

class Module
{
    public function getConfig(): array
    {
        $provider = new ConfigProvider();
        return [
            'view_helpers' => $provider->getViewHelperConfig(),
            'form_elements' => $provider->getFormElementConfig(),
        ];
    }
}

```

## In Module.php

_Module.php_

```auto
<?php
declare(strict_types=1);

namespace ModuleName;

class Module implements ViewHelperProviderInterface,FormElementProviderInterface
{
    public function getConfig(): array
    {
        return [
            'view_helpers' => $this->getViewHelperConfig(),
            'form_elements' => $this->getFormElementConfig(),
        ];
    }
    
    public function getViewHelperConfig(): array
    {
        return [
            'aliases' => [
            ],
            'factories' => [
            ],
        ];
    }
    
    public function getFormElementConfig()
    {
        return [
            'aliases' => [
            ],
            'factories' => [
            ],
        ];
    }
}

```

---

<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:** [May 25, 2024, 12:00am UTC](https://discourse.laminas.dev/t/best-practice-location-of-configuration/3703/2 "2024-05-25T00:00:24Z")

</div>

Module classes and ConfigProvider classes serve two different purposes.

Basically:  
Module classes provides support to Laminas MVC applications.  
ConfigProviders provide support to Mezzio applications.

In mezzio the ConfigProvider is added to the array to seed ConfigAggregator.

In MVC the module manager calls the Module.php files iirc based on the /config/autoload/modules.config.php or application.config.php at the application config level. Basically providing the list of modules that expose Module.php files to be called.

In components that support both the most common approach is to create an instance of ConfigProvider and return the providers method call as the MVC supported keys value through the getConfig method of the Module class.

Not sure if you have read this but it covers all things config.

> **[Advanced Configuration Tricks - tutorials - Laminas Docs](https://docs.laminas.dev/tutorials/advanced-config/)**
>
> Learn how to create laminas-mvc applications, get in-depth guides into components, and discover how to migrate your applications to version 3!

There is important information in this one as well.

> **[laminas-component-installer - Laminas Docs](https://docs.laminas.dev/laminas-component-installer/)**
>
> Composer plugin for injecting modules and configuration providers into application configuration
