Best Practice: Location of Configuration

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.

  • module.config.php
  • ConfigProvider.php
  • Module.php
0 voters

Array in module.config.php

module.config.php

<?php
declare(strict_types=1);

namespace ModuleName;

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

Module.php

<?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

<?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

<?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

<?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' => [
            ],
        ];
    }
}

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.

There is important information in this one as well.

1 Like