简体   繁体   中英

Determine which section a Zend_Config uses in a controller?

I have my config defined via Zend_Config_Ini adapter, with production, development and testing sections. In my controller I would like to take certain actions depending on what environment/section I am currently using - eg I only want to send email in production and testing, not in development.

Is there a way to determine which environment/section I am using the controller? I can set a value in the config and grab that in the controllers, but seems like there would be a cleaner way to determine.

If you put the code that depends on the environment in your controller, like this :

if ($env == 'production') {
    // send mail
}

It adds logic in the controller -- the sending of the mail doesn't depend on the configuration anymore.


I think it's better to add one mail.enable option in your configuration files, and use that specific option in your code, to determine whether or not you must send mails.
This way :

  • The configuration is the one which defines if mails should be sent or not
  • The day you want to send mails from your development environment, you just have to change one entry in your configuration file ; and not the code.


Of course, it means a couple more configuration entries (one to enable/disable e-mails, one for any other thing that can be enabled or not) ; but, in the end, that's what configuration is for.

When you followed the ZF quick start and set up your project using the command line tool, you have a constant called APPLICATION_ENV that you can use.

Eg

if(APPLICATION_ENV == "development")
{
  // custom logic here
}

That is getting defined in the index.php file in your public folder:

// Define application environment
defined('APPLICATION_ENV')
                || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM