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