Learning Joomla! 3 Extension Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Legacy MVC versus new MVC

Joomla! uses a Model-View-Controller (MVC) design pattern that separates the data from the presentation. The view displays all the data, but doesn't care how the data is stored; that's the job of the model. The controller tells the model and the views what to do.

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. Most people are still using the older MVC classes which have been renamed with the Legacy suffix. The core extensions in Joomla! 3 all use JControllerLegacy and JViewLegacy, and we will be focusing on the Legacy classes in this book.

So what is the point of this change? The idea was that the new MVC classes will be used in the CMS in a future version when it adopts Unified Content Model (UCM). The UCM idea is to have a single content structure that would be used for all the extensions, and this will allow them to interact more easily. Essentially, if you develop a comments plugin for one extension, it will automatically work for every other extension due to the consistent data structure. There is still a lot of work that needs to be done to implement UCM, but we may start seeing it around Joomla! 4. There are still not many good examples of the new MVC and the documentation is limited, so my advice is to stick with the legacy classes for now.

The Joomla! CMS roadmap released in March 2013 actually indicates that we will roll MVC legacy classes back to their original names and drop the Legacy suffix. Providing Joomla! continues with its backwards compatibility commitment throughout the Joomla! 3 series, and although the legacy classes may be marked as deprecated, they won't be removed until Joomla! 4.0. Any code we write now for Joomla! 3.1 should continue to work on Joomla! 3.2 and Joomla! 3.5. This MVC rollback may introduce a small backwards compatibility issue for those already using the new MVC that are using type hinting, for example if the new JModel was renamed to JModelInterface, so this is another reason why you wouldn't bother adopting the new MVC classes yet. You can read about the current roadmap at http://developer.joomla.org/cms/roadmap.html.

Using the legacy classes introduces a problem. What if you want to support Joomla! versions prior to Version 2.5.5? For example, Joomla! 2.5.4 doesn't have these legacy classes, so your extension is not going to work on that version. There are workarounds for this, for instance you could create a legacy.php file as follows:

<?php
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

class JControllerLegacy extends JController
{
}

jimport('joomla.application.component.view');

class JViewLegacy extends JView
{
}

jimport('joomla.application.component.model');

class JModelLegacy extends JModel
{
  public static function addIncludePath($path = '', $prefix = '')
  {
    return parent::addIncludePath($path, $prefix);
  }
}

Then you can load this following code into your component's main PHP file.

if (!class_exists('JControllerLegacy'))
{
  require_once( JPATH_COMPONENT_ADMINISTRATOR.'/legacy.php' );
}

This effectively does what Joomla! 2.5.5 has introduced, where the new legacy classes are an alias for the old ones. If you wanted to use the new MVC classes, then your extension will only be able to support Joomla! 3.0 or greater.

What about Joomla! 1.6 and 1.7, I hear you asking? Well, the above code would possibly make your extension work on these versions too. However there are quite a few other functions that have changed, so you are going to run into other problems. For example, JDate::toMysql() was removed in Joomla! 3 but its replacement JDate::toSql() wasn't introduced until Joomla! 2.5, so you would need to run a slightly different code to support these earlier versions. As Joomla! 1.6 and Joomla! 1.7 are short term releases that have already reached their expiry, and they both have known vulnerabilities, there is no point supporting these versions, and you should encourage anyone using these versions to upgrade to Joomla! 2.5 or even Joomla! 3.