Yii Framework Forum: actions in separte files as class - Yii Framework Forum

Jump to content

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

actions in separte files as class Rate Topic: -----

#1 User is offline   codescope 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 25-August 10

Posted 26 October 2011 - 11:41 PM

Hi

i was going through tutorial about separating actions from controller by putting them in separate file
for my case

i removed actionIndex from SiteController(default installation)
and create new file protected/controllers/site/IndexAction.php

what should i put in public function run(){

}

copying and pasting indexAction code from controller doesn't work.
0

#2 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,987
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 27 October 2011 - 01:46 AM

You can use your reciclable action in this way:

class SpecialController extends CController {

    public function actions() {
        return array(
            'someAction' => array(    /* index.php?r=special/someAction */
                'class' => 'ext.actions.YourParticularAction',
                'parameterName' => 'value'
            ),
            'someStuff' => array(    /* index.php?r=special/someStuff */
                'class' => 'ext.actions.YourParticularAction'
            ),
            ...
        );
    }

}

0

#3 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,987
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 27 October 2011 - 01:52 AM

Or alert your controller adding new actions by hand:

<?php

class YourclassController extends Controller {

    public $layout = '//layouts/column2';
    private $_model;

    public function actions() {
        return array(
            'aclist' => array(
                'class' => 'ext.YourAmathingAction',
                'model' => 'YourModel', /* ... if you want to pass a model */
                'say' => '33' /* ... if you want to pass a value */
            ),
        );
    }


that means:

<?php

class YourAmathingAction extends CAction {

    public $model;
    public $attribute;

    public function run() {
        $yourVar = ....
        /* from here you can read $this->model or $this->say ... or variable you pass from the controller */
        echo CJSON::encode($yourVar);
    }

}

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