New MVC for Joomla! CMS
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
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
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
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
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:
- Change site name
- Change site logo
- Change template colour
- Change background colour
- Put the site online/offline
- 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:
- Adding test for new MVC components (Would be able to get a good start on understanding new MVC)
- Converting back-end components into new MVC
- Converting front-end components into new MVC
- 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
Some articles published on the Joomla Community Magazine represent the personal opinion or experience of the Author on the specific topic and might not be aligned to the official position of the Joomla Project
By accepting you will be accessing a service provided by a third-party external to https://magazine.joomla.org/
Comments