How to use Zend\Config (application wide variables)
Asked Answered
S

1

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);
Slily answered 5/1, 2013 at 22:38 Comment(6)
possible duplicate of Zend Framework 2 module share variables between controllers onBootstrapIonogen
Don't use the session for such things, that's not the purpose of the session!Ionogen
@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 misunderstoodSlily
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.Ionogen
What confused me is that you have a $config-auth-user field, the config shouldn't contain user information.Ionogen
$config['auth']['user'] only stores the name/key $user is stored underSlily
A
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

Ablaut answered 6/1, 2013 at 0:49 Comment(7)
It seems it compiles from global.php and local.php files too (set in `root/config/autoload.config.php')Slily
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 timeSlily
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 ^^Ablaut
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 xDSlily
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: github.com/ZF-Commons/ZfcUser/blob/master/src/ZfcUser/Options/… and the factory here: github.com/ZF-Commons/ZfcUser/blob/master/Module.php#L90-L93Tally
const UPLOAD_PATH = getcwd() . '/public/uploads/'; why this is generating error?Strapless
@anil you can't use functions like getcwd() when assigning class constantsAblaut

© 2022 - 2024 — McMap. All rights reserved.