This section of the documentation contains an example to help you quickly understand this module. Suppose, we want to store theme settings which contains properties like font size, font color, background color etc.
First create domain object which represents theme settings:
<?php
namespace Application\Model;
class Theme
{
protected $fontSize;
protected $fontColor;
//.. other properties
public function setFontSize($fontSize)
{
$this->fontSize = $fontSize;
}
public function getFontSize()
{
return $this->fontSize;
}
public function setFontColor($fontColor)
{
$this->fontColor = $fontColor;
}
public function getFontColor()
{
return $this->fontColor;
}
//.. other methods
}
Now, tell HtSettingsModule about our new settings.
<?php
return [
'ht_settings' => [
'namespaces' => [
'theme' => [ // here `theme` is the namespace
'entity_class' => 'Application\Model\Theme',
'hydrator' => 'Zend\Stdlib\Hydrator\ClassMethods',
]
]
]
];
This is pretty straight forward. Here, theme
is a namespace. Namespaces allow us to manage multiple category of settings. So, you can handle different settings easily. And, the hydrator is for converting array settings to our domain object, Application\Model\Theme
.
<?php
$settings = new Application\Model\Theme();
$settings->setFontSize(25);
$settings->setFontColor('red');
$settingsManager = $this->getServiceLocator()->get('HtSettingsManager');
$settingsManager->save($settings, 'theme'); // done
$this->settings()->save($settings, 'theme');
$themeSettings = $this->getServiceLocator()->get('HtSettingsManager')->getSettings('theme');
echo $themeSettings->getFontSize(); // will print 25
echo $themeSettings->getFontColor(); // will print red
You can also use HtSettingsModule\Service\SettingsAbstractFactory
to get settings directly from service manager.
// module.config.php
return [
'service_manager' => [
'abstract_factories' => [
'HtSettingsModule\Service\SettingsAbstractFactory',
]
]
];
So, you can get settings directly from service manager like this:
$themeSettings = $this->getServiceLocator()->get('settings.theme');
$themeSettings = $this->settings('theme');
$themeSettings = $this->settings('theme');