Take the 2-minute tour ×
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no registration required.

I'm curious as to where to get data from JInput in the MVC design pattern. For example, in a situation where I need a JInput value to use in a query in a model class method, should I get the JInput value there or should I pass it from the view or does it matter?

share|improve this question

3 Answers 3

up vote 8 down vote accepted

In a proper MVC structure, only your controller should interface with your request data, in this case JInput, and the dependencies injected into the view and model when instantiated within the controller.

With the current CMS structure, all three pieces of the MVC structure are often interfacing with the request data. In models, this is usually done in the populateState() method and views are more often in the layout files they are loading up than the actual class.

share|improve this answer
    
Thanks for the reply Michael. Would you mind explaining why it should theoretically be in the controller? –  doovers Apr 30 at 5:39
1  
Agree with Michael. Only the Controllers should be aware of request data. Model: Your DATA. View: The thing that controls how your data is PRESENTED. Controller: Sits between the data and the view, other data sources, and controls any changing of the data that needs to be done. –  Dmitry Rekun Apr 30 at 5:54
    
It absolutely makes sense to me why request data should only be retrieved in the controller but is this even possible with the current Joomla MVC? Example: If you want to display a single item based on ID, you call $this->get('Item') in your View class to retrieve the data from your model. In your model's getItem method you could then get the ID from the request data ($id = JFactory::getApplication()->input->get('id')) and bring that ID into your SQL statement. Is there another way to do that? –  fl0r Apr 30 at 10:16
    
In the current architecture, no, it isn't possible to get that level of separation. –  Michael Apr 30 at 16:12
    
@fl0r another way is to retrieve ID in the Controller and then to update the Model's state. –  Dmitry Rekun May 1 at 14:35

I'm of the strong belief that it should be retrieved where appropriate.

That is to say you should always use JInput otherwise you're duplicating data and potentially breaking DRY principles.

A view class should be retrieving data to populate the the view's templates (/tmpl files) or layouts from the model, not injecting values into the model. A view after all is about presentation of data and UI not manipulation of data or handling of user actions.

share|improve this answer
    
You might as well abandon the MVC completely when not following the rules! –  sovainfo May 1 at 15:13
    
Agreed… if we were talking about a real MVC implementation, but with Joomla's implementation of the MVC this isn't possible, see @Michael's answer below. –  cppl May 1 at 21:58
    
The whole purpose of MVC is doing things where they are supposed to. The fact that Joomla MVC is not 'real MVC' doesn't mean you should throw all rules overboard. –  sovainfo May 1 at 22:30
    
I've tried to follow Joomla's examples per the core components which use populateState() in their models almost exclusively, hence my answer. –  cppl May 1 at 23:57
    
Maybe I misread 'where appropriate' in your answer. Nothing against following core, even when they are not according to 'real MVC'. –  sovainfo May 2 at 10:59

JInput doesn't offer such feature; so you might have to use $_POST.

if you can have the input be in the form of array (and use JInput::getArray() )

or

a json-encoded object (you use json_decode(JInput::getString()))

http://developer.joomla.org/manual/chap-Joomla_Platform_Manual-Input.html

http://docs.joomla.org/Retrieving_request_data_using_JInput

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.