where to put reusable functionality?

Hi guys.

I’m a newbie in Yii.

I have gone through the docs and understand controller-model-view thing very well, but there’s one thing I’m missing.

What about reusable functionality?

For ex. there’s a blog application and BlogController has loadBlog function, that just loads currently viewed blog:

$this->_model = Blog::model()->findbyPk($id!==null ? $id : $_GET[‘id’]);

Now I have a widget which is supposed to display a list of ‘related’ blogs - it will extrat current blog tags and search for other blogs which have same tags.

So in my widget I will need that loadBlog function - should I copy/paste it or how do I make this functions reusable in multiple controllers?

Thank you.

You could create a static helper class (eg MyFunctions.php) and add it in protected/components folder.




class MyFunctions {


public static function getBlog($params){


}


}



Import it in your config if it’s not imported by *




'import'=>array(

   'application.components.MyFunctions',

  ),



and then call the function from any controller





MyFunctions::getBlog($params);




Excellent, thank you!