开发者

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 t开发者_如何学Co take certain actions depending on what environment/section I am currently using - e.g. 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.

E.g.

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'));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜