The Joomla! Community Magazine™

New MVC for Joomla! CMS

Written by | Friday, 01 November 2013 00:00 | Published in 2013 November
With the upcoming Joomla! CMS releases, the new MVC will play a major role. Through my Google Summer Of Code project, I've taken the initial steps for that.

Introduction

Joomla! CMS is a world famous CMS which uses MVC (Model-View-Controller) architecture to make its components. It has been there for many years and has gone through many changes to suite different kinds of needs. For more information about current MVC implementation, you could refer the Joomla! Documentation [1] and [2]. Very recently Joomla! Developers had the idea of making this architecture more flexible to overcome future challenges. As a result of that, this new MVC is introduced to Joomla! CMS.

First look at the New MVC

If you are new to Joomla! Component development, it would be beneficial to go through existing MVC architecture once. For those who have already involved in developing components, it'll be easy to identify the differences. This section will guide you through the changes one by one.

New folder structure

Folder Structure

It's almost similar to existing MVC architecture. But folder names are singular. So in new MVC you are expected to use singular folder names.

Single file for a controller

Controllers

This is an important change in new MVC. In new MVC has a separate file for each controller. There are special advantages of this design and will be discussed in the article later. In the existing MVC there's only one file containing all these controlling tasks.

Single method for a controller

Controller Execute

Inside a controller, structure is very simple. It's just a one method, called "execute". The full functionality of that controller is aggregated in to that "execute" method. This change made controllers very easy to understand and access.

New libraries for MVC

Libraries

For the new MVC, libraries/joomla folder contains 3 new subfolders; controller, model and view. Each folder contains respective Base class, which must be used in implementing component's controller, model and view classes. Currently these are included within latest Joomla! CMS release. These library level classes forms the foundation for any new MVC component [3]. The main advantage of this is to reduce the code redundancy in Legacy classes, which are using by existing MVC.

As time goes on more reusable class will be added to the libraries. You can see some examples of possible reusable controllers in the controller folder of com_config [4] or the cms folder of the com_cache-com_checkin repository [5].Thanks to autoloading, in some cases components will not require any controllers of their own or will not require one per class. Moreover you will see View classes are also become very simpler and Model classes are similar to those in existing MVC. In new MVC the Model class no need to be a child of JModelBase class, but also can be a table class depending on the requirement.

Advantages of New MVC

While going through creating Joomla! Components using new MVC, as a developer you will feel following facts.

Fact

Existing MVC

New MVC

Support For Web Services

No

Yes

Learnability

Hard

Easy

Controllers, Models and Views

Fairly Complex

Very Simple

Extend using

Legacy classes

Base classes

Number of class files

Less

Vary on component


At this moment following components have transferred into new MVC and waiting in Pull Request queue.

  • com_config [4]
  • com_checkin [5]
  • com_cache [5]

Front-End administration

Up to now I have shown you the importance of new MVC. Now you may like to see some special achievements through this change. This front-end administration is one such thing I have already implemented with new MVC. Through front-end administration, Joomla! CMS users will get the chance of do various changes without accessing backend at all. I believe this would be very useful to attract first time users, as the back-end (administrative view) contains very complex configuration panels.
As the initial step, I was able to successfully create front-end administration view for Global Configuration and Template Manager. Following are some features offering by those front-end administration components:

  1. Change site name
  2. Change site logo
  3. Change template colour
  4. Change background colour
  5. Put the site online/offline
  6. Add Meta description

By the time there will be more capabilities will be add to front-end administration.

What developers say

"The new MVC let's us really simplify code dramatically and increases the flexibility of applications, especially in terms of delivering web services as well as html. When you combine that with JApplicationWeb that is very powerful. Having some shared MVC classes in the CMS will mean making new extensions and applications much easier."  ~Elin Waring, Former President at Open Source Matters

"In terms of using the new MVC interfaces, which was designed to be application agnostic, versus the 'legacy' MVC, which was designed to support the CMS, the newer interfaces allow for a greater level of flexibility by developers. The issue tracker application is a great example of how you can implement the interfaces and web routers to keep your code as light as practical. " ~Michael Babker, Joomla! Production Leadership Team

"The new Joomla MVC is going to make dev life much easier. This project take Joomla few steps forward and make Joomla Framework come alive. The new MVC make it fast to build more versatile apps and the new front-end for config is evidence for that." ~Ofer Cohen, Board Member & Code contributor, Open Source Matters

How can you contribute?

If you are interested about new MVC, of course you also can contribute to this. Still conversion to new MVC is halfway through and need to do more to get the full benefit of this architecture. Therefore contributions can be done in following forms:

  1. Adding test for new MVC components (Would be able to get a good start on understanding new MVC)
  2. Converting back-end components into new MVC
  3. Converting front-end components into new MVC
  4. Writing your own extensions., helping to adapt some of the other packages in the libraries that don't work perfectly with it yet.(eg: For example com_menus needed a slight update to work with the new style.)

References

[1] Model-View-Controller - http://docs.joomla.org/Model-View-Controller
[2] Developing a MVC Component/Introduction - http://docs.joomla.org/J3.1:Developing_a_MVC_Component/Introduction
[3] Joomla! Platform Documentation MVC - http://joomla.github.io/joomla-platform/?chapters/packages/mvc.md
[4] Implementing com_config in new MVC - https://github.com/joomla/joomla-cms/pull/2063
[5] Implementing com_cache and com_checkin in new MVC - https://github.com/joomla/joomla-cms/pull/1989

 

Read 18613 times
Tagged under Google Summer of Code, English
Buddhima Wijeweera

Buddhima Wijeweera

I’m a final year student in Department of Computer Science and Engineering, at University of Moratuwa, Sri Lanka. I have already did contributions to Joomla! project and Apache Synapse project. I'm interest to learn and experience web application related stuffs.

avatar
Thanks for the good explanation of the changes!
VOTES:1
avatar
Thanks for the info.
VOTES:0
avatar
Mihail Semerdzhiev Friday, 15 November 2013
How this will affect the update to next version of Joomla?
VOTES:0
avatar
Hi there,
I have just one question: if a component has a complex behaviour, for example needing to save different kind of data from the backend (i.e. articles and categories), in the "old" MVC it would have multiple controllers with a save() task for each one. What happens in the "new" MVC, where there is only one controller and only one "save" task (in the "save.php" controller, called "execute")?

Thanks!
VOTES:1
avatar
Buddhima Wijeweera Friday, 15 November 2013
Hi Anas,
According to new MVC you have to create separate directories for each kind of data. Then each directory has a separate file for a controller.
For example, let's take back-end com_config, which is already transformed in to new MVC.
Link: github.com/joomla/joomla-cms/tree/master...om_config/controller
It has 2 different kinds Application and Component.

I think the above example will help you to solve the question.

Thank You!
VOTES:0
avatar
Buddhima Wijeweera Friday, 15 November 2013
Hi Mihail,
This implementation will not affect updating to latest version or the functionality of components.
This new MVC is focused to make developers life easier when building components and make Joomla! MVC structure more flexible.

Thank You!
VOTES:0
avatar
great news..specially for me. Now customizing Joomla site will be lot straight forward with this new MVC implementation that I am familiar with from long time. Old MVC in Joomla was really harder. New implementation is very straight forward. Very Happy...On top of that developers who are use to with other frameworks will feel blessed as it would be lot easier to try Joomla.
One thing will this new MVC implementation supports HMVC features?
VOTES:0
avatar
Thanks a lot,

A really great explanation for all developers !
VOTES:0
avatar
I love new MVC and already use it for a while.
VOTES:0
avatar
One of the major disadvantages I see in the new MVC is that it lacks controllers with multi-task support.

Consider the migration of a component like com_banners. What will the save controller do? Save clients or save

banners? So you need to split things... split clients and banners in two components or more directories... More files to maintain, more things to refactor. The screenshots with the file system that you present are an ideal scenario. A real component does not look like this.

I've already used the new MVC for a CRUD component and another one which was more Web Services oriented. The new MVC is so light and modern that you end up maintaining extra code (basically more code than for the legacy classes), time consuming and really not worth it for such a scenario (except for the learning experience).

Don't get me wrong, I really do appreciate the time and effort put into your work. But I would not take com_config as an final reference example ... The component structure is a mess (or maybe say work in progress) with folders

like controller and controllers and view and views. Not to mention the base classes - really fat models- that are maintained (from what I saw) per component.

Yes, there are also lots of goodies inside, like for example partial views, which I absolutely love and the

improved architecture which gives lots of flexibility. But flexibility must not be confused with productivity.

This is one of the things that looks good on paper, introduced without getting community feedback and without

having a clear upgrade path. It provides a more modern architecture but fails to solve some basic needs that extension developers have.
VOTES:1
avatar
Hi Valentin Despa,

In the article I'm showing an example directory structure. Because it's very easy to explain and understand.
For the com_banner; client, clients, banner, banners and tracks will be separated in to separate directories.
Number of files increased a bit, but provide more flexibility by assigning single responsibility for each controller.
Meanwhile it also improves understandability and maintainability of controllers.
But for simple deflections like save and apply, you can use a controller.
Still this is work-in-progress, but you can see that there's no Cancel controller for front end com_config/config, which says in future some common tasks can be achieved through reusing.
Joomla CMS has a deprecation process. So I can't remove 'controllers', 'models' at once, which may cause problems for 3rd party developers. Ignore those directories when you try to get an idea about com_config.
This new MVC structure was introduced to Joomla! sometime back and there are tutorials about how to create components using it.
In com_config project, I used new MVC to refactor com_config. Beyond that, some parts are identified as carrying out common tasks by many controllers and taken into generalized level.
All the discussions during the refactoring went through joomla feature tracker and github as well. (Link: github.com/joomla/joomla-cms/pull/1445)
Those generalized classes are still resides in front end 'com_config/controller', and will be transferred to library level in future.

As a conclusion I can say, since this produces a component with more understandability and maintainability, the productivity will be increased eventually.

Thank You!
VOTES:1
avatar
I'm already using new MVC in several of my projects.

A benefit is that we can create a nested structure of controllers. For example, in the past, apply() always call save() in it inner or register apply task to save() method, then save() method has to add
if ($this->task == 'apply')
to redirect back to edit page.

But in new MVC, we just make
ControllerApply::execute
extends from
ControllerSave::execute
, bingo! We just call parent and add a redirect, other behavior are same as save(). A beautiful flow.

I think we're just need some useful "JControllerDefault" like "JControllerDisplay", "JControllerBatch" or "JControllerSave", then, if some task are not match any controller in component, just use the default controllers in library. This may helpful for developer.


But I have a question, is new MVC has a technology name? Like XXMVC etc. Or or inspired from where? Or we just call it NEW MVC?
VOTES:0
avatar
Hi Simon,
Thank you for your appreciations.
Definitely new MVC will help to chain controllers and help HMVC(Hierarchical MVC).
I've already implemented some of those library controllers inside front end com_config/controller. And kept those inside com_config as this is still work-in-progress. But any developer free to use those.

There's no special name for this MVC. We call this new MVC, because there exists another MVC in Joomla! (legacy one).
This is a variation of MVC which included single responsibility principle into controllers.
Thank You!
VOTES:0
avatar
Hi!
I'm planning to start learning Joomla programming. I've found a book called 'Learning Joomla 3 extension development (3th edition)' @ PacktPub, published at July 2013, so it's totally new. But there's a sinister sentence in it at chapter called 'Before you start':

"New MVC classes were introduced in Joomla! Platform 12.1 which were added to the Joomla! CMS for Joomla! 3 and Joomla! 2.5.5, but these new classes still need a bit of work before they will become widely adopted. ... we will be focusing on the Legacy classes in this book."

Does that mean that it's totally useless to learn from this brand new book? Or is there a well-definable part of the content that changed by moving from Joomla Platform to Joomla Framework?

What would you suggest for people who don't know anything about Joomla programing but want to learn it?
VOTES:-1
avatar
Buddhima Wijeweera Sunday, 22 December 2013
Hi,
First I would like to welcome you to Joomla Programming!
If you are willing to learn through new MVC, there are examples available online and can get the advantages I mentioned.
But, if you need to learn building components with old MVC, it'll not be a waste since Joomla 3 supports that.
There are ongoing projects which refactoring core extensions to new MVC.
Therefore, there's a good chance to learn new MVC, by getting involved with refactoring some of the core extensions.
Thank You!
VOTES:1
avatar
Has this stuff been merged into the final Joomla codebase currently. I was looking through com_cache and com_config and still seeing references to the Legacy stuff.
VOTES:0
avatar
Buddhima Wijeweera Thursday, 23 April 2015
Hi Eric,

This is currently in the code base in com_config:
github.com/joomla/joomla-cms/tree/stagin...ntroller/application

Confusion is still "controllers" folder remains for backward compatibility.

Thank You!
VOTES:0