How Joomla MVC Differs from other Frameworks

Because Model-View-Controller is a design pattern and a specific piece of code, different coders choose to implement it in different ways. Joomla is no exception. While Joomla's implementation of MVC holds to the same general pattern as other frameworks, there are a few noteworthy differences.

view.html.php - First, let's take a look at view.html.php. You may have noticed that view.html.php loads the view and the model in the same file. Most other frameworks out there that you will find often try to do this in the controller. Joomla views are responsible for getting the model data. In many other frameworks, you will find that the controller will load the model and then load up the data and then pass that data over to the view, while in Joomla that's all handled in this view.html.php file. So you can really think of view.html. php as a sort of mini-controller that's just focused on doing things for the view. This way Joomla controllers stay very small and that's very desirable because you'll often find people talk about MVC and say that you want to have lean controllers and fat models and this way, your controllers stay very, very lean and the views are just fed directly from the models.

Another difference is that there is a completely separate backend and frontend of your component - This is great from a security standpoint because when you have your code in that administrator/component/common_name of your component, you know that that code is not at all ever going to be displayed or used on the frontend. So it's great in separating out code that's only for admins as opposed to code that's for public consumption. Unfortunately, this is a trade- off because it is lousy from a reusability standpoint. You will have instances where you want to display a list of records from the database and that code is generally going to be the same in the frontend as in the backend may be with one or two small slight changes. Unfortunately, you end up having to duplicate all this bliss code because the code is completely separate in the backend from the front end.

Another difference you will find is that a lot of other frameworks are concerned with what's known as the front controller - A lot of people talk about the front controller problem and what they're saying is that when you load up your web application, you need to come up with a way of telling the framework which controller you want to load and Joomla handles this elegantly. You can either specify which component you want to load in the URL using that option variable, or you can create a menu item link and that menu item link will determine which component gets loaded. So your component ends up acting as the front controller because then Joomla will just load whichever component you specify, and then it will load the controller for that component.

However, Joomla also supports multiple controllers per component and when you get into multiple controllers then you have to choose which controller in that component you want to use and Joomla handles this by overloading the task variable into two parts.The first part is the name of the controller, and then you have a period, and then you have the name of the task. So if you have a task that is ever in that format where you have a title and then a period and then another title, Joomla is automatically going to break that out and assume the first part is the name of the controller and the second part is the name of the task you want to run.

Another difference between Joomla and many other frameworks is that the MVC design pattern is not even required - You can just do a quick script in that PHP file where your component starts and never load a model, a view, or a controller. You can just write straight a PHP and go with it. This is really good for when you're trying to do maybe a data import, or you just try to test something out very quickly. It's nice to be able to just start writing PHP and not have to worry about MVC while you get something working. You can also just pull in the pieces of MVC that you want. You don't have to pull in a model, a view, and controller every time. A lot of other frameworks will force you to have the output in the view and that really makes them kind of brittle when you want to do something, it's a little bit simpler, for instance, when you're getting JSON data.

Let's take a look at some examples here.


                      
  1. class NoidaCityController extends JController
  2. {
  3. public function get_json_malls()
  4. {
  5. $model = $this->getModel('Malls');
  6. $malls = $model->getItems();
  7. echo json_encode($malls);
  8. }
  9. }

                      

So in Joomla, we have a controller here where we're getting mall data in JSON format and we're just getting the mall's model, we are getting the items from the mall's model, and then we are immediately outputting in JSON format the mall data. And this way we are only doing one line of code here that's producing output. So it's very compact and it gets to the point. It's what we want. we just want JSON data back out of the browser. We are not going to lay this out in a specific way and it's very simple and straightforward. So this is a really good feature that lets you output that JSON without a lot of trouble.

However, what you will find with other frameworks is that they are very strict about where you put your output. So other frameworks might do code that's more like this.


                      
  1. //controller
  2. class NoidaCityController
  3. {
  4. public function get_json_malls()
  5. {
  6. $model = $this->getModel('Malls');
  7. $malls = $model->getItems();
  8. $view = $this->getView('json');
  9. $view = display($malls);
  10. }
  11. }

                      

You have first your controller where you're getting the mall data from the model, and then you are getting your view and assigning the mall data to the view, and then you create one more file where you finally echo out the json_encode data call.


                      
  1. //view
  2. <?php
  3. echo json_encode($data);

                      

And this in my mind is a little bit of a waste because all we are trying to do is echo out the data in JSON format, no matter what the data is and we are never going to use markup. So what is the point of creating a separate view file? That's why I prefer Joomla So while Joomla's implementation of MVC may stray from that of other frameworks, it's still fundamentally the same structure.

Joomla gives you the flexibility of deciding whether you even want to use a MVC at all or only part of it. Knowing the nuances of Joomla's MVC implementation will make it easy to pick up other frameworks.