php - Zend Framework 2: Disable module on production environment

827

OK, here's the problem:

I have a module in my Zend Framework 2 application that I wish to not include on production. Therefore, I made a file insideconfig/autoload calledlocal.php with the following content:

'modules' => array(
    'Application',
    'My_Local_Module',
),

whileconfig/application.config.php contains:

'modules' => array(
    'Application',
),

When I try to access the module in the URL, a404 is returned. However, when I set themodules inside theapplication.config.php file, the module is displayed properly. The environment variable is set tolocal.

585

Answer

Solution:

Put the following lines in your index.php:

<?php

// ...

// Get the current environment (development, testing, staging, production, ...)
$env = strtolower(getenv('APPLICATION_ENV'));

// Assume production if environment not defined
if (empty($env)) {
    $env = 'production';
}

// Get the default config file
$config = require 'config/application.config.php';

// Check if the environment config file exists and merge it with the default
$env_config_file = 'config/application.' . $env . '.config.php';
if (is_readable($env_config_file)) {
    $config = array_merge_recursive($config, require $env_config_file);
}

// Run the application!
Zend\Mvc\Application::init($config)->run();

Then create different configuration files for each environment.

application.config.php:

<?php

return array(
    'modules' => array(
        'Application'
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php'
        ),
        'module_paths' => array(
            './module',
            './vendor'
        )
    )
);

application.development.config.php:

<?php

return array(
    'modules' => array(
        'ZendDeveloperTools'
    )
);

application.production.config.php:

<?php

return array(
    'module_listener_options' => array(
        'config_cache_enabled' => true,
        'module_map_cache_enabled' => true,
        'cache_dir' => 'data/cache/'
    )
);
939

Answer

Solution:

You have to enumerate all your modules inside application.config.php, so the config should look like that:

$modules = array (
    'Application'
);

if (IS_LOCAL_DOMAIN)
{
    $modules [] = "My_Local_Module";
}

return array(
    'modules' => $modules,
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

People are also looking for solutions to the problem: php - putting the array data into separate variables

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.