Sven
Sven

Reputation: 13295

Zend Framework 2: Custom config as Zend\Config\Config or plain PHP array, what is the recommended way?

In Zend Framework 2 I wrote a module with custom configuration options.

The options are obtained using the service locator and are then passed to the object that is being initialized inside a factory.

Since my configuration is in PHP array format, I could just pass the array, or I could first create a new instance of Zend\Config\Config and pass this to the new object.

I tried both ways & they both work, but what is the recommended way of doing this inside ZF2 & why? Does it has any advantages to use Zend\Config\Config?

Especially since I just noted it's possible to cast the Config back to an array using toArray() I am curious of possible benefits.

Upvotes: 2

Views: 117

Answers (3)

Artek Wisniewski
Artek Wisniewski

Reputation: 797

Most of the modules just add to the global config, that is accessible via $serviceManager->get('config')

This way your config can be cached later on via config_cache_enabled in application.config.php and users can override config options using their local configurations.

If your's module config isn't related to the global config mechanism it's really up to you on how you store/manage it. Keeping it as an array is simpler, whereas using it as Zend\Config\Config lets you use various tools for config (ini writers, database etc).

Upvotes: 0

jcropp
jcropp

Reputation: 1246

If I'm developing a project for others, I like to give them a certain level of access to the configuration so that they do not have any reason to delve into the code themselves. I'll create a table in the database called something like 'settings' or 'config' that the admin can access through a form, and the data populates a php configuration array. In order to do this, the PHP array format is the way to go.

("a" recommendation, not necessarily "the" recommended way)

Upvotes: 0

jhansen
jhansen

Reputation: 284

By using the object you would be keeping with the OOP inherent to Zend, plus it has functionality not available to a basic array, and it can be extended to use custom business logic if the need arises.

IE, maybe a condition comes up that will change one of the configuration options, but the rest are the same.

Upvotes: 1

Related Questions