I have some configuration on local/config/xxx/config.ini and a local/config/xxx/config_base.ini.
How can I access at there configuration on Zend Framework 2. I have an instance of the ServiceProvider but I cannot access at these config. It only ready the module.config.php (ex with $config = $sm->get('Config');)
I found that I can use this for read an ini file:
$config = (new Zend\Config\ReaderIni())->fromFile(getcwd() . '/local/config/xxx/config.ini');
But then how to merge the two configurations?
Why would do you use .ini extension for your config files? As you can read in the documentation on Advanced Configuration Tricks config files should be stored as .php files. They contain arrays holding the keys and values of your configuration and they will be automatically read from the autoload folders and merged with the module configuration files.
Once configuration is aggregated from all modules, the ConfigListener will also merge application configuration globbed in specified directories (typically config/autoload/).
Here you can also find the folder pattern used to find the files it needs to merge:
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively overide configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
'config/autoload/{{,*.}global,{,*.}local}.php',
),
The advantage is that you don't need a separate file reader and you can access your custom config from the service manager directly like you would do for other configs.
$config = $sm->get('Config');
$webhost = $config['my_param'];
Another advantage is that you can setup your production environment to cache the config files which will dramatically increase the performance of the bootstrapping procedure (reading files is a slow process).
I would suggest you rename your file extension to .php and follow the documentation for setting up your custom configuration values.
When using the skeleton application, the system configuration is by default in config/application.config.php.
/ config.php
return array(
'webhost' => 'www.ekio.rw',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.ekio.rw',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
Then,
// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');
// Display a configuration datum (results in 'www.ekio.rw')
echo $config->webhost;
Related
I use a global array to define certain configurations for my application. But so far I've read that globals are bad practice and should be avoided. This global is read-only, so I'm not altering it in any way in my code.
What would be the correct way of defining configuration settings?
$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'database'
),
'navigation' => array(
'Home' => array('/', '/index.php'),
'Sign up' => array('/signup', '/signup.php'),
'Log in' => array('/login', '/login.php')
)
);
Start with a Configuration class to hold all settings in a collection of key->value pairs and getter and setter methods. This class is part of the normal application code.
To be able to handle different settings for each environment, like the connection parameters in your example, use multiple files.
For each environment a folder exists to store the all environment-specific configuration files.
One of the files stored here is a script that:
creates a static configuration object
sets all configuration parameters to the values for that environment.
Other parts of the application use this static object to get the settings from.
The files in the different folders have equal names, for example in this structure:
- config
- development
- config.php
- staging
- config.php
- production
- config.php
During deployment the configuration files for the environment that you are deploying to are included. What I do is also put the deployment scripts in these folders.
This setup supports multiple environments and you can keep the files with configuration settings under version control.
I don't have much of an idea of PHP. But with respect to OOP, you can create a class(possibly named Config) which contains static read-only variables. And then use them as follows
Config.mysql and Config.navigation
I have a few modules and the Zend Framework library in "C:\Public\vendor" folder. The directory looks like this:
vendor
--> Module1
--> Module2
--> ZF2
----->library
------->Zend
In my PHP application, which is actually located in "C:\Public\apps\myApplication", I am able to get the Zend Framework path, by setting a ZF2_PATH environment variable in my apache's httpd-vhosts.conf file.
However, to get the Modules to load, in application.config.php, I am having to mention the physical path of the module location (which might change in a production environment), ex:
'module_paths' => array(
'./module',
'./vendor',
'C:\\Public\\vendor',
),
Is there a way to set and retrieve, module_paths using an environment variable in apache?
Why forget about normal PHP just when working with a Framework?
$modulePaths = array();
switch ($env) {
case 'local' : $modulePaths[] = ....;
// other cases ....
}
'module_paths' => $modulePaths,
I modified my code like this to read the environment variable, set in apache and it worked..
'module_paths' => array(
'./module',
'./vendor',
getenv('CUSTOM_MODULES_PATH'),
),
Thanks for your input Sam..
I am starting a new project.
I am using ZF2. I have just installed it and have the Skeleton application up and running.
This is my deployment process:
I develop on my local machine
I then push to my public github repository
I then use deployhq.com to deploy those to my production
server which is where the user would see the changes made.
I have tried to look around stack, zend site, and google at blogs etc but still dont have any real understanding or solution to my problem.
I want the application to use different database credentials based on its environment. E.g. if on 'dev', my local machine, then use credentials A, but if on live server, then use credentials B.
I have read a lot about global and local autoload config files etc, but baring in mind my github repo is public, any where I commit any config files with my db details would be visible.
I was wondering if there was a way to have, the same theory, global and local files with the DB connections in, i upload the production details manually, not via git for security reason, and tell git to ignore the local config file somehow? I would also need to know how I tell the application to use those config files based on the environment and there location.
In Zend 2 There are
Global configuration file &
Module level configuration file
IF you want to know there use you can refer the link below
How does configuration works in ZF2
When I had a same scenario I used the above link to understand and exploit Zend Config module which is really good to handle the situation like this .
create two files
production.php
local.php
in both these files
return this array based on the environment
return array(
"dbname" => "yourdbname"
"dbhostname" => "dbhostname"
"dbusername" => "yourdbusername",
"dbpassword" => "yourdbpassword"
);
in config/autoload/ directory of your zend framewrok application
later edit your config/application.config.php file as per below instructions
// get the application env from Apache vhost file ( here you can set in your apache vhost file as production or local )
$applicationEnv = getenv('APPLICATION_ENV');
$environmentSpecificConfigPath = "config/autoload/{,*.}{".$applicationEnv.",local}.php";
// Next with in the config array pass the environment specific configuration path
'config_glob_paths' => array($environmentSpecificConfigPath)
in any controller or action
you can just use the below code
$configArray = $this->getGlobalConfig();
Now $configarray has all your DB credentials to create a connection
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => $configArray['dbname'],
'username' => $configArray['dbusername'],
'password' => $configArray['dbpassword']
));
If you use config array to connect the DB in your entire application
you dont need to worry about environment changes just make sure you have an Apache APPLICATION_ENV entry in your vhost file
you can do that by adding below line in your apache vhost file
SetEnv APPLICATION_ENV "production" // in your production server
SetEnv APPLICATION_ENV "local" // in your local
Also Last but not least you can use the Zend Experts module ZeDB
https://github.com/ZendExperts/ZeDb
To manage your CRUD applications
Hope the above steps may help you in creating the environment
I've been looking for information on this question but the only answer I can find is by looking at how other modules take care of this. So far, I have seen this:
With CdliTwoStageSignup in Module.php
'factories' => array(
.
.
'cdlitwostagesignup_module_options' => function($sm) {
$config = $sm->get('Configuration');
return new Options\ModuleOptions($config['cdli-twostagesignup']);
},
.
}
With ZfcUser in Module.php
'factories' => array(
'zfcuser_module_options' => function ($sm) {
$config = $sm->get('Config');
return new Options\ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());
},
Based on Zend 2 documentation, ModuleManager Merges all the module.config.php of each module and is set in the service manager. Also, config files in .config/autoload directory can override the module config files.
To access the configurations, these two modules seem to use the keywords: "Config" and "Configuration".
Are these always the the keywords used with the service manager to get to config files?
Is there any difference between choosing one over the other?
Appreciate any answer you can provide.
The keywork used should be config. Internally, afaik, all key, aliases, etc.. will be turned from CamelCase to dash-separated-lowercase. So Config equals config as far as configuration is concerned.
Now when it comes down to configuration vs. config i assume configuration was left available as an alias to config. At one point, i think beta 4 or beta 5 they've decided that all occurences of configuration will be replaced by the well known abbreviated form of config. If ever there is any occurence of configuration that not acts as a fallback to config all community will appreciate you opening an issue on github to get it fixed.
In the sceleton application that I've downloaded from github there is a file
module/Application/config/module.config.php
return array(
'layout' => 'layout/layout.phtml',
'display_exceptions' => true,
'di' => array(
'instance' => array(
'alias' => array(....
this file is used in module/Application/module.php:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
How to create 3 different configs depending on domain (production, staging, development)? It seems in ZF1 env vars has been used, but I don't know how to do that in zf2 module.
Thank you!
Create a file called development.config.php in application/config/autoload and this will be loaded after all the modules' config files have been loaded. As a result, you can override anything the merged configuration by adding the relevant keys to this file.
The name of the file loaded is {APPLICATION_ENV}.config.php, so you can create production.config.php, etc.
Note that you may have to change the glob in index.php as it's unclear if the Skeleton application will work out of the box with APPLICATION_ENV or not at this stage of the development of ZF2 (early April 2012).
it seems to work with a simple .htaccess change. :
SetEnv APPLICATION_ENV development
I don't know if staging will work, but production and development work out of the box.
I think it works through the event listener, but don't ask me how, I haven't gotten that far yet.