How to override the component mvc from the Joomla! core
(Created page with "Tutorial") |
Javiparati (Talk | contribs) (→Using application scope method) |
||
| (9 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | ===NOTICE=== | |
| + | This method only works if you install and enable the 3rd party MVC plugin - or provide your own equivalent plugin. It is fine for advanced developers - just be aware that this is not part of Joomla! Core code | ||
| + | |||
| + | ===Summary=== | ||
| + | There may be times when you need to override the Joomla! MVC Component to add new functionality or to completely customise the component. The MVC component can be completely overridden by using the same class names that are used in the component. This is achieved by loading a system plugin before the component is dispatched by the application. | ||
| + | |||
| + | You can create your own plugin to do this. | ||
| + | |||
| + | <code> | ||
| + | class plgSystemComContentOverride extends JPlugin { | ||
| + | public function __construct(&$subject, $config = array()) { | ||
| + | parent::__construct($subject, $config); | ||
| + | } | ||
| + | |||
| + | public function onAfterRoute() { | ||
| + | $app = JFactory::getApplication(); | ||
| + | if('com_content' == JRequest::getCMD('option') && !$app->isAdmin()) { | ||
| + | require_once(dirname(__FILE__) . DS . 'comcontentoverride' . DS . 'my_content_controller.php'); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ------------------------ | ||
| + | |||
| + | In the examples below we are using an [http://extensions.joomla.org/extensions/style-a-design/templating/15611 Override MVC plugin] | ||
| + | |||
| + | ------------------------ | ||
| + | |||
| + | == Getting a head-start with overrides == | ||
| + | |||
| + | You can override by templating or by application scope (frontend/backend). | ||
| + | |||
| + | == Using a templating method == | ||
| + | |||
| + | Override Base Component Controller | ||
| + | <source lang="xml">TEMPLATE_NAME/code/COMPONENT_NAME/CONTROLLER.php</source> | ||
| + | Override Another Component Controller | ||
| + | <source lang="xml">TEMPLATE_NAME/code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php</source> | ||
| + | Override Models | ||
| + | <source lang="xml">TEMPLATE_NAME/code/COMPONENT_NAME/models/MODEL_NAME.php</source> | ||
| + | Override Views | ||
| + | <source lang="xml">TEMPLATE_NAME/code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php</source> | ||
| + | |||
| + | == Using application scope method == | ||
| + | |||
| + | You can override by creating a <code>code</code> folder in the base path from application. | ||
| + | |||
| + | '''Note''': this method is experimental. Please read this https://groups.google.com/forum/?fromgroups=#!searchin/joomla-dev-cms/override/joomla-dev-cms/_vVxbHsXm20/JdsBRP8xrPAJ and this first http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=29031 | ||
| + | |||
| + | '''Site General Override''' | ||
| + | |||
| + | <source lang="xml">code/COMPONENT_NAME/CONTROLLER.php</source> | ||
| + | Override Another Component Controller | ||
| + | <source lang="xml">code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php</source> | ||
| + | Override Models | ||
| + | <source lang="xml">code/COMPONENT_NAME/models/MODEL_NAME.php</source> | ||
| + | Override Views | ||
| + | <source lang="xml">code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php</source> | ||
| + | |||
| + | '''Administrator General Override''' | ||
| + | |||
| + | <source lang="xml">administrator/code/COMPONENT_NAME/CONTROLLER.php</source> | ||
| + | Override Another Component Controller | ||
| + | <source lang="xml">administrator/code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php</source> | ||
| + | Override Models | ||
| + | <source lang="xml">administrator/code/COMPONENT_NAME/models/MODEL_NAME.php</source> | ||
| + | Override Views | ||
| + | <source lang="xml">administrator/code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php</source> | ||
| + | |||
| + | '''Extending Default Class''' | ||
| + | |||
| + | if you ENABLE the override class functionality you can extend original classes and override your custom methods. | ||
| + | |||
| + | See this example with banner controller: | ||
| + | |||
| + | file: administrator/code/com_banners/controller.php | ||
| + | <pre> | ||
| + | /** | ||
| + | * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. | ||
| + | * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| + | */ | ||
| + | // No direct access | ||
| + | defined('_JEXEC') or die; | ||
| + | /** | ||
| + | * Banners master display controller. | ||
| + | * | ||
| + | * @package Joomla.Administrator | ||
| + | * @subpackage com_banners | ||
| + | * @since 1.6 | ||
| + | */ | ||
| + | class BannersController extends BannersControllerDefault | ||
| + | { | ||
| + | //your custom methods here | ||
| + | }</pre> | ||
| + | <noinclude>[[Category:Development]][[Category:Tutorials]]</noinclude> | ||
Latest revision as of 12:38, 14 February 2013
Contents |
[edit] NOTICE
This method only works if you install and enable the 3rd party MVC plugin - or provide your own equivalent plugin. It is fine for advanced developers - just be aware that this is not part of Joomla! Core code
[edit] Summary
There may be times when you need to override the Joomla! MVC Component to add new functionality or to completely customise the component. The MVC component can be completely overridden by using the same class names that are used in the component. This is achieved by loading a system plugin before the component is dispatched by the application.
You can create your own plugin to do this.
class plgSystemComContentOverride extends JPlugin {
public function __construct(&$subject, $config = array()) {
parent::__construct($subject, $config);
}
public function onAfterRoute() {
$app = JFactory::getApplication();
if('com_content' == JRequest::getCMD('option') && !$app->isAdmin()) {
require_once(dirname(__FILE__) . DS . 'comcontentoverride' . DS . 'my_content_controller.php');
}
}
}
In the examples below we are using an Override MVC plugin
[edit] Getting a head-start with overrides
You can override by templating or by application scope (frontend/backend).
[edit] Using a templating method
Override Base Component Controller
TEMPLATE_NAME/code/COMPONENT_NAME/CONTROLLER.php
Override Another Component Controller
TEMPLATE_NAME/code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php
Override Models
TEMPLATE_NAME/code/COMPONENT_NAME/models/MODEL_NAME.php
Override Views
TEMPLATE_NAME/code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php
[edit] Using application scope method
You can override by creating a code folder in the base path from application.
Note: this method is experimental. Please read this https://groups.google.com/forum/?fromgroups=#!searchin/joomla-dev-cms/override/joomla-dev-cms/_vVxbHsXm20/JdsBRP8xrPAJ and this first http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=29031
Site General Override
code/COMPONENT_NAME/CONTROLLER.php
Override Another Component Controller
code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php
Override Models
code/COMPONENT_NAME/models/MODEL_NAME.php
Override Views
code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php
Administrator General Override
administrator/code/COMPONENT_NAME/CONTROLLER.php
Override Another Component Controller
administrator/code/COMPONENT_NAME/controllers/CONTROLLER_NAME.php
Override Models
administrator/code/COMPONENT_NAME/models/MODEL_NAME.php
Override Views
administrator/code/COMPONENT_NAME/views/VIEW_NAME/VIEW_NAME(.FORMAT).php
Extending Default Class
if you ENABLE the override class functionality you can extend original classes and override your custom methods.
See this example with banner controller:
file: administrator/code/com_banners/controller.php
/**
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die;
/**
* Banners master display controller.
*
* @package Joomla.Administrator
* @subpackage com_banners
* @since 1.6
*/
class BannersController extends BannersControllerDefault
{
//your custom methods here
}