1

Main question is self explanatory, but I'll give some side examples:

  • I'm having trouble figuring out databases, though it seems I can work out config with ServiceManager stuff

  • I want to use constants for cookie names, so I can change them easily if there are collisions. Current, I'm calling $config = new \Zend\Config\Config(include $_SERVER['DOCUMENT_ROOT'] . '/../config.php'); every time I want access to my global config.php file. A lot of previous solutions were in Zend 1 (eg Zend_Registry). Is this the right way to do it? It seems a little unwieldy using that over and over.

  • Is there a way to utilize a Module's configuration file to set module-wide-variables/constants?

  • Unless I'm completely missing it, there's no application.ini in Zend 2

  • Storing recaptcha public/private keys

  • I'm also using my config file for session-variables (same idea as $_SESSION[CONST_NAME]), which makes it really clumsy with the config file above. Is it better to hardcode the session names? Like:


$container = new Zend\Session\Container('auth');
$container->offsetSet('user', $user);
... // instead of 
$container = new Zend\Zession\Container($config['auth']['containername']);
$container->offsetSet($config['auth']['user'], $user);
Raekye
  • 5,081
  • 8
  • 49
  • 74
  • possible duplicate of [Zend Framework 2 module share variables between controllers onBootstrap](http://stackoverflow.com/questions/14128085/zend-framework-2-module-share-variables-between-controllers-onbootstrap) – markus Jan 05 '13 at 23:06
  • Don't use the session for such things, that's not the purpose of the session! – markus Jan 05 '13 at 23:07
  • @markus-tharkun what should I use then? I remember in one of the big "how to login user" (can't find the exact threads) on SO it said to use sessions, unless I completely misunderstood – Raekye Jan 05 '13 at 23:23
  • Yes, that's right. Some data of the associated user is totally fine to store in the session... I was under the impression it was more but I was wrong. – markus Jan 05 '13 at 23:42
  • What confused me is that you have a $config-auth-user field, the config shouldn't contain user information. – markus Jan 05 '13 at 23:43
  • $config['auth']['user'] only stores the name/key $user is stored under – Raekye Jan 05 '13 at 23:44

1 Answers1

2

All configuration from each module.config.php or Module.php are put together into a big pot. You can easily access those via $this->getServiceLocator()->get('config')

When it comes down to constants, they should be placed inside the respective classes. Like

class UserStorage {
    const SESSIONCONTAINERNAME = 'blubbusersession';
}

That way you can call \My\User\Model\UserStorage::SESSIONCONTAINERNAME whenever you need this info

As far as your example is concerned thought, there should be almost no need to var-code your session-container-name because the information from your modules session-data should be made available via your modules Service-Classes. But if you still need it, see aboves example.

Furthermore i think it may be a good idea for you to check out how zf-commons\ZfcUser does things

Sam
  • 16,435
  • 6
  • 55
  • 89
  • It seems it compiles from global.php and local.php files too (set in `root/config/autoload.config.php') – Raekye Jan 06 '13 at 01:00
  • And btw, I have numerous other questions. I work through them for at least a day before asking (usually an new approach comes up overnight), but mostly it seems there's a lot of support (previous questions) for Zend 1, but not Zend 2. Stuff like how to elegantly pass data to the layout (again, I found a clumsy solution) and how to check if an action was forwarded. Because these are not previously-asked questions for zend 2, is it alright to ask them all here? Like I should figure it out in the long run, but it'd save me a lot of time, and possibly future users some time – Raekye Jan 06 '13 at 01:07
  • First you are right. ALL Config-Files are merged together ultimately. And of course you may ask it all here. Someones gotta do the job of asking questions ^^ – Sam Jan 06 '13 at 09:13
  • Alright, I didn't want to sound like I hadn't tried anything, because I still have a lot of questions! Stay tuned for more xD – Raekye Jan 06 '13 at 09:22
  • 2
    Also, try not to mix options from one module into another one. Keep them strictly separated. And for re-usability of options and to have them directly casted to the correct type, try to make an `Options` class. Check the class here: https://github.com/ZF-Commons/ZfcUser/blob/master/src/ZfcUser/Options/ModuleOptions.php and the factory here: https://github.com/ZF-Commons/ZfcUser/blob/master/Module.php#L90-L93 – Jurian Sluiman Jan 06 '13 at 10:08
  • const UPLOAD_PATH = getcwd() . '/public/uploads/'; why this is generating error? – Katty Dec 28 '16 at 11:38
  • @anil you can't use functions like `getcwd()` when assigning class constants – Sam Dec 28 '16 at 16:42
  • @Sam Thank you! I was not aware with this. – Katty Dec 29 '16 at 08:06