Book Image

Learning Joomla! 3 Extension Development - Third Edition

By : Timothy John Plummer
Book Image

Learning Joomla! 3 Extension Development - Third Edition

By: Timothy John Plummer

Overview of this book

Joomla 3 is the first of the major open source content management systems that was meant to be mobile friendly by default. Joomla uses object-oriented principles, is database agnostic, and has the best mix of functionality, extensibility, and user friendliness. Add to that the fact that Joomla is completely community driven, and you have a winning combination that is available to everyone, and is the perfect platform to build your own custom applications. "Learning Joomla! 3 Extension Development" is an integrated series of practical, hands-on tutorials that guide you through building and extending Joomla plugins, modules, and components. With Joomla having been downloaded well over 35 million times, there is a huge market for Joomla extensions, so you could potentially earn some extra cash in your spare time using your newly acquired Joomla extension development skills. We will start with developing simple plugins and modules, and then progress to more complex backend and frontend component development. Then we will try our hand at ethical hacking, so you will learn about common security vulnerabilities and what you can do to avoid them. After that we will look at how you can prepare your extensions for distribution and updates, as well as how you can extend your components with various plugins and modules. Finally, you will end up with a fully functioning package of extensions that you can use on your own site or share with others. If you want to build your own custom applications in Joomla, then "Learning Joomla! 3 Extension Development" will teach you everything you need to know in a practical, hands-on manner.
Table of Contents (18 chapters)
Learning Joomla! 3 Extension Development
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

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.