Search code examples
phpyiiyii-extensionsyii-components

How to import Yii Controller in Yii widgets


How to import Yii Controller in Yii widgets;

My Widget code is:

class RecentBlogsWidget extends CWidget { 


public $showblogs;


public function run() {

    Yii::import("application.components.Controller");

    $model = Blog::model()->activeBlogsWidget($this->showblogs)->findAll();

    $this->render('RecentBlogs',
                     array(
                            'model' => $model,
                            )
                );
}

}

Now I call my component/controller function $this->getAuthor($blog->author_id) in this widget view but giving me error:

RecentBlogsWidget and its behaviors do not have a method or closure named "getAuthor".


Solution

  • I just imported my Controller in my

      views/RecentBlogsWidget.php     
      Yii::import('application.components.Controller'); 
    

    and then call my method like this:

      Controller::getAuthor($blog->author_id);
    

    it is working now for me.