Search code examples
phpzend-frameworkzend-config

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 - 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.


Solution

  • 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.