简体   繁体   中英

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 ). But I need to pass a Joomla sub controller to a specific view file (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. I think this is the most standard sequence to implement MVC in Joomla:

  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; 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; you can however choose to invoke $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.

You can however simply redirect passing the params in the 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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