Global functions

Where are the places we can put global application functions? Similar to the "application helpers" in Rails?

You can create a component, simply a class in component, and then create static methods.

This approach is similar to CHtml, so I think is compliant with the framework style

If the helper needs internal housekeeping, use a CApplicationComponent, otherwise just a static class.

The application component is added like other components in the main config, and then you can access the object everywhere.

Small, global helper functions are best tucked in a static utility class IMO.

I tend to encapsulate global functions to specific objects… for example, those related to Language settings, manipulation and so on… I do it in an component named Language. If it needs to deal with DB then I put the funcitons on the related model. Being models you can access functions as ModelName::model()->functionName, and if static ModelName::functionName().

And of course, you could even import a file with global functions (Yii::import()). At the end of the day, make it so it is easy to maintain and you feel comfortable with.

IMHO of course!


In my opinion the component with static methods will be most flexible and extendable in future.