【问题标题】:How to pass variable from controller to the view joomla mvc如何将变量从控制器传递到视图 joomla mvc
【发布时间】:2013-04-07 12:43:23
【问题描述】:

如何根据这个例子将我的变量从 joomla 子控制器传递到视图

    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();
    }
    }

【问题讨论】:

    标签: model-view-controller view joomla controller


    【解决方案1】:

    在控制器中:

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

    在视图中:

    class MycomponentViewItem extends JViewLegacy
    {
      /** @var  string   my variable */
      public $myVariable;
    
      public function display($tpl = null)
      {
        $myVariable = $this->myVariable;
        //...
      }
    }
    

    【讨论】:

    • @pirtr_cz 如何在布局中使用这个 $myVariable?
    • @Harsimran > 试试$this->myVariable,因为布局是视图的一部分
    • Notice: Undefined property: HostingViewHost::$myVariable
    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2012-02-10
    • 2012-06-08
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多