Yii Framework Forum: Importing Controller Methods - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Importing Controller Methods Rate Topic: -----

#1 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 09 February 2010 - 09:55 AM

I have a function that assembles an English sentence about a model object. For example, "Joe created a forum topic 3 days ago". I would like to use this function/method in views for that model that are imported elsewhere. It works fine in my views as $this->sentence(), but I'm not sure how to reference it when it is imported into another controller. In other words, it works fine in the ContentController, where it is defined, but if I import the model in the SiteController, it can no longer be referenced as $this->sentence.

This is how I've done it so far, in the SiteController.php file. It works, but I don't think it is the proper way.
public function sentence( $content_item ) {
    include_once( 'ContentController.php' );
    $ctlr = new ContentController('content'); // TBD: Not sure if this is the proper way to do this.
    return $ctlr->sentence( $content_item );
  }

0

#2 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 09 February 2010 - 10:47 AM

View Postqueej, on 09 February 2010 - 09:55 AM, said:

I have a function that assembles an English sentence about a model object. For example, "Joe created a forum topic 3 days ago". I would like to use this function/method in views for that model that are imported elsewhere. It works fine in my views as $this->sentence(), but I'm not sure how to reference it when it is imported into another controller. In other words, it works fine in the ContentController, where it is defined, but if I import the model in the SiteController, it can no longer be referenced as $this->sentence.

This is how I've done it so far, in the SiteController.php file. It works, but I don't think it is the proper way.
public function sentence( $content_item ) {
    include_once( 'ContentController.php' );
    $ctlr = new ContentController('content'); // TBD: Not sure if this is the proper way to do this.
    return $ctlr->sentence( $content_item );
  }



look this http://www.yiiframew...ion.integration
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 09 February 2010 - 12:29 PM

IMO this method should be put somewhere into the Model domain as it's coupled to your model objects. So you could again create a base model and extend your models from that:

class BaseRecord extends CActiveRecord {

    public function sentence()
    {
        // your sentence logic here. No need to supply the content_item, as you can use $this instead
    }

}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users