How to pass variable from controller to the view joomla mvc

喜你入骨 提交于 2019-12-07 02:30:23

问题


How do I pass my variable from joomla sub-controller to the view according to this example

    class MYControllerControllerParser extends JController{

            public function __construct($default = array()) {

            parent::__construct($default);

        }

     protected function _import($file, $type) {

            $layout = '';
            switch ($type) {

                case 'importcsv':
                    $contains_headers       = false;
                    $field_separator    = JRequest::getVar('separator');
                    $field_separator    = empty($field_separator) ? ',' : $field_separator;
                    $field_enclosure    = JRequest::getVar('enclosure');;
                    $field_enclosure    = empty($field_enclosure) ? '"' : $field_enclosure;
//this variable should be passed to the view
                    $this->info = $this->getImportInfoCSV($file, contains_headers, $field_separator, $field_enclosure);
//This variable should go to view
                    $this->file = basename($file);
                    $layout = 'importcsv';
                    break;
            }

    $this->getView('import','html')->display();
    }
    }

回答1:


In Controller:

$view = $this->getView('import','html');
$view->myVariable = 'hello';
$view->display();

In View:

class MycomponentViewItem extends JViewLegacy
{
  /** @var  string   my variable */
  public $myVariable;

  public function display($tpl = null)
  {
    $myVariable = $this->myVariable;
    //...
  }
}


来源:https://stackoverflow.com/questions/16014548/how-to-pass-variable-from-controller-to-the-view-joomla-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!