I have seen many examples to pass data from a Joomla controller to views (e.g. 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?
-
why can't you use the subcontroller to render the view? subcontroller should have all of its parents behavior – DevZer0 Aug 09 '13 at 10:36
-
No machan if i just need to render the view i could do it with $this->setRedirect(JRoute::_('path to my view'));. but I could not find a way to pass some data with it and access them in the view – Ruwantha Aug 09 '13 at 10:46
-
when you say sub-controller your saying something that Extends JController right? – DevZer0 Aug 09 '13 at 10:48
-
check here https://groups.google.com/forum/#!topic/joomla-dev-general/O_T0tqbiU4Q if you haven't already that is – DevZer0 Aug 09 '13 at 10:52
-
Yes it its grand parent is the Jcontroller I dose not help – Ruwantha Aug 09 '13 at 10:58
-
I cannot understand what does that means to pass even controller to view. Anв what is sub-controller? There is not anything like that in joomla. What Joomla version are you using? It is quite different in 2.5 and 3.0 – Sergey Romanov Aug 09 '13 at 11:39
-
What do you mean by there is noting like that(subcontrollr). Refer joomla docs.http://docs.joomla.org/JController_and_its_subclass_usage_overview#Subcontrollers. BTW. I'm using joomla 2.5 – Ruwantha Aug 09 '13 at 12:06
-
@SergeyRomanov usually by subcontroller people mean the individual controllers in the controllers folder as opposed to the controller in the extension root folder. – Elin Aug 09 '13 at 12:35
-
I manage to transfer the data with a session. But I'm not satisfied with that solution. There go to be a better way!!! – Ruwantha Aug 09 '13 at 16:33
1 Answers
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:
- The controller reads the input and sets the relevant parameters in a session variable
- The controller redirects to the view
- The view loads the model
- 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();
}

- 5,590
- 1
- 20
- 36
-
I am communicating with models in $model->getData2($param1,$param2) way. My question was to communicate view and controller. I think there should be a way to do this without sessions. – Ruwantha Aug 13 '13 at 04:38
-
well you can setRedirect(JRoute::_('index.php?option=...¶m1=something¶m2=somethingelse; or you can instantiate the view manually from the controller and invoke its render() method. – Riccardo Zorn Aug 13 '13 at 18:45
-
Yes these are good. Thanks. But I still wish if there is a function like all other frameworks. – Ruwantha Aug 14 '13 at 05:30
-
Please check my updated answer, I put in a sample of how to invoke the view directly from the controller – Riccardo Zorn Aug 16 '13 at 07:38