Loading ZendDeveloperTools Only in Development
I'm finally jumping feet first into ZF2 and one of the feature's I'm still not sure about is modules. Ideally, they break your code up into reusable chunks but it they still seem to have some drawbacks. For example, I'm a big fan of making sure a page is created as fast as possible (because performance is a feature) so I love the fact that there is a module (the ZendDeveloperTools module which I've mentioned before) that displays a bar at the bottom of the screen with various statistics about the page including how long it took to generate the page. The downside to the module system is that by enabling the module in the application.config.php file
Zend will run code specific to that module even in environments where it won't ever be used (like production). Below is a simple fix to only load specific modules on your local instance.
Step 1 is to create an application.local.config.php
file in your config directory with the contents listed below. In this case, this is only going to include the additional module that you want loaded but it could be expanded to include additional settings. Do not include this file in your version control system or it defeats the whole purpose of doing this. :-)
<?php
return array(
'modules' => array(
'ZendDeveloperTools',
),
);
This next step is where all the magic is going to happen. In your project's index.php
file there is a line that normally looks like this:
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
We're going to alter this so instead of just using the base application.config.php
file we're going to load the base file and then merge in our changes using array_merge_recursive
and then finally run the application.
// load the configuration
$config = require 'config/application.config.php';
// load the local config and merge
if(is_readable('config/application.config.local.php')){
$localConfig = require 'config/application.config.local.php';
$config = array_merge_recursive($config, $localConfig);
}
// Run the application!
Zend\Mvc\Application::init($config)->run();
At some point I need to look into caching this result but for small configs it shouldn't make that much of a difference.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows