Global property in controller

Hi there!

This may be a bit embarassing, but how can I best make a property value available to all my actions in a specific controller?

Let’s assume I have following controller:




class UploadController extends Controller

{

    public function actionIndex() {           

        if ($handle = opendir($FOLDER)) {

            // [...]

        }

        $this->render('index');

    }


    public function actionSend() {

        $myfile = $FOLDER . DIRECTORY_SEPARATOR . $file;

        // [...]

    }

}



And $FOLDER should be something like


$FOLDER = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "media" . DIRECTORY_SEPARATOR . "uploads"

Where should I put $FOLDER = […] so I can access it from actionIndex() and actionSend()?

Normally I would declare $_folder as a protected property and assign the value in the __constructor, but when I use a __constructor, Yii can’t find my index template any more. I guess because the __constructor of the base CController gets overwritten.

Regards

ycast

I’d probably use something like this:




private $_folder;

public function getFolder()

{

  if ($this->_folder===null)

    $this->_folder=dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "media" . DIRECTORY_SEPARATOR . "uploads";

  return $this->_folder;

}



Due to CComponents accessor methods you can then simply use $this->folder everywhere in your controller/views.

Nice, didn’t know Yii supports that. Works as expected, thanks :)