简体   繁体   English

如何从Joomla子控制器传递数据到视图?

[英]How to pass data from Joomla sub controller to view?

I have seen many examples to pass data from a Joomla controller to views (eg here ). 我已经看到很多例子将数据从Joomla控制器传递给视图(例如这里 )。 But I need to pass a Joomla sub controller to a specific view file (view.html.php). 但我需要将Joomla子控制器传递给特定的视图文件(view.html.php)。 I searched about it for a whole day and did not found a solution. 我搜索了一整天并没有找到解决方案。 Does anyone know how to do this? 有谁知道如何做到这一点?

Joomla MVC is very loose and you can implement this behaviour in several ways. Joomla MVC非常松散,您可以通过多种方式实现此行为。 I think this is the most standard sequence to implement MVC in Joomla: 我认为这是在Joomla中实现MVC的最标准序列:

  1. The controller reads the input and sets the relevant parameters in a session variable 控制器读取输入并在会话变量中设置相关参数
  2. The controller redirects to the view 控制器重定向到视图
  3. The view loads the model 视图加载模型
  4. The model reads the params from the session. 该模型从会话中读取参数。

But you could handle the params in 3. and pass them on to the model; 但你可以处理3中的参数并将它们传递给模型; this really is a matter of style/taste. 这真的是风格/品味的问题。 Since Joomla allows you to invoke your model from the view with $this->get('Data') for example, there is no room for passing params; 由于Joomla允许您使用$ this-> get('Data')从视图中调用模型,因此没有传递params的空间; you can however choose to invoke $model->getData2($param1,$param2). 但是你可以选择调用$ model-> getData2($ param1,$ param2)。

The basic calls are: 基本电话是:

JApplication::getUserStateFromRequest()

which in a single call reads the input and falls back on the previously saved session data; 它在一次调用中读取输入并返回先前保存的会话数据;

setUserState to persist this info in the session and getUserState to be used in the model to retrieve the data. setUserState在会话中保留此信息,并在模型中使用getUserState来检索数据。

You can however simply redirect passing the params in the url; 但是,您可以简单地重定向传递URL中的参数; then use the view.html.php to parse the input and set the internal state of the model before calling methods ($model->setState), or avoid redirect entirely and load the models and view from the controller (which seems a more standard and easy approach to MVC, but is seldom seen in Joomla). 然后使用view.html.php解析输入并在调用方法($ model-> setState)之前设置模型的内部状态,或者完全避免重定向并从控制器加载模型和视图(这似乎更标准轻松接近MVC,但在Joomla中很少见到。

Directly invoking the view from the controller 直接从控制器调用视图

    $vName      = 'yourview';
    $vFormat    = 'html'; // raw

    if ($view = $this->getView($vName, $vFormat)) {
        $model = $this->getModel($vName);
        $model->setState('filter.type', $type);
        $view->setModel($model, true);

        // Push document object into the view.
        $view->assignRef('document', $document);

        $view->display();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM