title: Return value from external action in Yii
body: <p>How to pass a value from external action in Yii to it's parent controller?</p>
  
  <p>for example:
  External action looks like:</p>
  
  <pre><code>&lt;?php
   class uploadAction extends CAction
  {
  /**
  * Runs the action.
  * This method is invoked by the controller owning this action.
  */
  public function run()
  {
      .....
  
          $fileName=$result['filename'];//GETTING FILE NAME
  
          $this-&gt;controller-&gt;image = $fileName; // this line does not work!!  
  
  }
    }
  </code></pre>
  
  <p>when I try to get the value of images in the parent controller, nothing return!! Any help I will appreciate.</p>
  
  <p>Update:
  I am using an extension to upload a file .. <a href="http://www.yiiframework.com/extension/eajaxupload/" rel="nofollow">eajaxupload </a>.</p>
  
  <p>There is a long form.. have many fields, one of them is image. I want to upload that image by Ajax before submiting the whole form. Of course after user click on create button.. the controller must take all fields plus image name to store in the db.
  The view ..</p>
  
  <pre><code>&lt;div class="elem"&gt;
              &lt;?php echo $form-&gt;labelEx($model,'username'); ?&gt;
              &lt;?php echo $form-&gt;textField($model,'username',array('class'=&gt;'inputbox grid-11-12','maxlength'=&gt;45)); ?&gt;
              &lt;?php echo $form-&gt;error($model,'username'); ?&gt;
          &lt;/div&gt;
  
          &lt;div class="elem"&gt;
              &lt;?php echo $form-&gt;labelEx($model,'password1'); ?&gt;
              &lt;?php echo $form-&gt;passwordField($model,'password1',array('class'=&gt;'inputbox grid-11-12')); ?&gt;
              &lt;?php echo $form-&gt;error($model,'password1'); ?&gt;
          &lt;/div&gt;
  
          &lt;div class="elem"&gt;
              &lt;?php echo $form-&gt;labelEx($model,'password2'); ?&gt;
              &lt;?php echo $form-&gt;passwordField($model,'password2',array('class'=&gt;'inputbox grid-11-12')); ?&gt;
              &lt;?php echo $form-&gt;error($model,'password2'); ?&gt;
          &lt;/div&gt;
  
          &lt;div class="elem"&gt;
              &lt;?php echo $form-&gt;labelEx($model,'email'); ?&gt;
              &lt;?php echo $form-&gt;textField($model,'email',array('class'=&gt;'inputbox grid-11-12','maxlength'=&gt;45)); ?&gt;
              &lt;?php echo $form-&gt;error($model,'email'); ?&gt;
          &lt;/div&gt;
  
          &lt;div class="elem"&gt;
              &lt;label for="content"&gt;User Image:&lt;/label&gt;
          &lt;?php $this-&gt;widget('ext.EAjaxUpload.EAjaxUpload',
              array(
              'id'=&gt;'uploadFile',
              'config'=&gt;array(
                     'action'=&gt;Yii::app()-&gt;request-&gt;baseUrl .'/backend.php/user/upload',
                     'allowedExtensions'=&gt;array("jpg"),//array("jpg","jpeg","gif","exe","mov" and etc...
                     'sizeLimit'=&gt;3*1024*1024,// maximum file size in bytes
                     'minSizeLimit'=&gt;50*1024,// minimum file size in bytes
                     'multiple'=&gt;false,
                     //'onComplete'=&gt;Yii::app()-&gt;request-&gt;baseUrl .'/backend.php/user/saveStuff/?fn='. "js:function(id, fileName, responseJSON){ alert(fileName); }",
                     //'messages'=&gt;array(
                     //                  'typeError'=&gt;"{file} has invalid extension. Only {extensions} are allowed.",
                     //                  'sizeError'=&gt;"{file} is too large, maximum file size is {sizeLimit}.",
                     //                  'minSizeError'=&gt;"{file} is too small, minimum file size is {minSizeLimit}.",
                     //                  'emptyError'=&gt;"{file} is empty, please select files again without it.",
                     //                  'onLeave'=&gt;"The files are being uploaded, if you leave now the upload will be cancelled."
                     //                 ),
                     //'showMessage'=&gt;"js:function(message){ alert(message); }"
            )
              )); ?&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>This is the controller ..</p>
  
  <pre><code>    &lt;?php
  
      class UserController extends Controller
      {
  /**
   * @var string the default layout for the views. Defaults to '//layouts/column2',     meaning
   * using two-column layout. See 'protected/views/layouts/column2.php'.
   */
  public $layout='//layouts/column1';
  public $image;
  
  .......
  
  
  public function actions()
  {
      return array(
          'upload' =&gt; array(
          'class' =&gt; 'ext.actions.uploadAction',
          ),
          );
  }
  
  ........
  
  
  public function actionCreate()
  {
      $model=new User;
      $profile=new UserProfile;
      // Uncomment the following line if AJAX validation is needed
      // $this-&gt;performAjaxValidation($model);
  
      if(isset($_POST['User']))
      {
          $model-&gt;attributes=$_POST['User'];
          $profile-&gt;attributes=$_POST['UserProfile'];
  
          if(!$this-&gt;saveUser($model, $profile))
              Yii::app()-&gt;user-&gt;setFlash('error', 'Not Saved :)!');
  
      }
  
      $this-&gt;render('create',array(
          'model'=&gt;$model,
          'profile'=&gt;$profile,
      ));
  }
  
  public function saveUser($model, $profile)
  {
      $userValid  = $model-&gt;validate();
      $profileValid  = $profile-&gt;validate();
      $valid = $userValid &amp;&amp; $profileValid;
      if($valid)
      {
          $model-&gt;save(false);
          $profile-&gt;user_id = $model-&gt;id;
          $profile-&gt;image = !is_null($this-&gt;image)?  $this-&gt;image : null; // name of image file which uploaded 
          $profile-&gt;save(false);
  
          Yii::app()-&gt;user-&gt;setFlash('success', 'Saved :)!');
          $this-&gt;redirect(array('index'));
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>the external action in details is:</p>
  
  <pre><code> &lt;?php
   class uploadAction extends CAction
   {
  /**
  * Runs the action.
  * This method is invoked by the controller owning this action.
  */
  public function run()
  {
      Yii::import("ext.EAjaxUpload.qqFileUploader");
          // make the directory to store the pic:
          $folder=Yii::getPathOfAlias('webroot') .'/images/' . $this-&gt;controller-&gt;id . '/';// folder for uploaded files
          if(!is_dir($folder))
          {
             mkdir($folder);
             chmod($folder, 0755); 
             // the default implementation makes it under 777 permission, which you could possibly change 
              //recursively before deployment, but here's less of a headache in case you don't
          }
  
          $allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
          $sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
          $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
          $result = $uploader-&gt;handleUpload($folder);
          $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  
          $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
          $fileName=$result['filename'];//GETTING FILE NAME
  
          $this-&gt;controller-&gt;image = $fileName; // ??????
  
          echo $return;// it's array
  
  
  
  }
  }
  </code></pre>