2

In config file I have stored address to server my website rely on.

In case of failure of this server I want to use backup server. (permanently, not only for this request). So if the server1 fails, I want to use server2.

I found the best way to change adress in my config file. But as I know Zend Framework 2 can not write to config files.

Any ideas?

  • 1
    What does "failure of the server" mean. If it fails, will you still able to read variables from the config on your your server? It seems very illogical to store the redirect logic inside the application config of the server that needs redirected when "failing"... – Wilt Jan 06 '16 at 08:34
  • Those 2 servers are from external provider. I use them for storing multimedia content. Sometimes one of those server fails, or the requests start to timeout. It took some time for administrator of those servers to get it fixed.... I do not want my users to wait (on every request) for server1 to timeout and then use server2... I want to store to some permanent storage, that server 1 failed, so my website uses server 2 before it gets fixed. – Jakub Šesták Jan 07 '16 at 09:49
  • 1
    In that case you should maybe also consider using a queuing service so the user doesn't have to wait at all. When using a queuing service the server will process the job after returning a response to the client. – Wilt Jan 07 '16 at 09:51

2 Answers2

0

But as I know Zend Framework 2 can not write to config files.

Yes it can. It has whole Zend\Config\Writer component. If you are using default php array config files, you can alter config file like this:

$path = 'config/autoload/local.php';
$config = include $path;
$config['key'] = 'new value';

$writer = new \Zend\Config\Writer\PhpArray();
$writer->toFile($path, $config);

Do not forget to delete the config cache, if you are using one.

But I think there is better way to do this - instead of altering config files, you could prepare config template for each server and easily swap them. So in config/autoload directory you would have files adresses.local.server1.php.dist and adresses.local.php.dist containing address config for every server you need. Then if you need to use config for server1, just copy it:

$ cp adresses.local.server1.php.dist adresses.local.php
lku
  • 1,732
  • 14
  • 20
  • Config files should be used for static variables. It seems very illogical to change a variable using a writer before reading and using it. – Wilt Jan 06 '16 at 08:31
  • @Witt I agree. That's why I offered another solution. – lku Jan 06 '16 at 09:14
0

Config files should be used for static variables only.

To implement this it would be better to make a service that returns you the address to the server. For example a ServerAddressProvider.

$service = $serviceManager->get('Application\Service\ServerAddressProvider');
$address = $service->getServerAddress();

Inside your service you can add your custom logic that returns the correct server address.

Inside the server you could for example ping the server prior to returning the address to check if it is online and return an alternative (backup) server if the server is momentarily not available.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Thanks for another solution. Do you mean ping the server before every request? That would give me extra latency. And in case of failure of the server I would have to wait for ping to timeout.... not? – Jakub Šesták Jan 07 '16 at 10:15
  • How else do you know whether the server is online/offline? How do you do that in your current solution. About the extra latency, that is why I suggested implementing a queuing service in [my comment](http://stackoverflow.com/questions/34617436/zend-framework-2-write-to-config-file/34651912#comment57049040_34617436). Your clients should not be waiting for your server to handle storing your multimedia content. – Wilt Jan 07 '16 at 11:02