© 2015 X2Engine Inc.

Difference between revisions of "Customization Framework"

From X2Engine
Jump to: navigation, search
(Custom App Configuration)
Line 26: Line 26:
  
 
= Custom App Configuration =
 
= Custom App Configuration =
The configuration for the Yii application can also be customized through the files "web.php", "console.php" and "test.php" in <tt>custom/protected/config</tt>. These files will be included and run in the default configuration files for X2Engine running as a web application, console application or unit testing environment (respectively).  
+
The configuration for the Yii application can also be customized through the files "web.php", "console.php" and "test.php" in <tt>custom/protected/config</tt>. These files will be run in the default configuration files for X2Engine running as a web application, console application or unit testing environment (respectively).  
  
 
An array which mirrors the modified parts of main.php (the default configuration file) should be returned within the custom config file. To customize the configuration of the application, i.e. to add parameters to <tt>Yii::app()->params</tt>, one only need add/remove/alter the values and keys within <tt>$config</tt>.
 
An array which mirrors the modified parts of main.php (the default configuration file) should be returned within the custom config file. To customize the configuration of the application, i.e. to add parameters to <tt>Yii::app()->params</tt>, one only need add/remove/alter the values and keys within <tt>$config</tt>.

Revision as of 17:34, 2 April 2014

Introduction

X2Engine's source code is completely transparent and totally under your control. There's no need to compiled PHP, so altering your software is a simply a matter of hacking around in the files. Traditionally, this meant directly altering the source code within X2Engine's file structure. But this could be problematic to keep track of, given the vast number of files. On top of that, automatic updates to any of those files would wipe out all of your changes, so you would have to choose between customization and receiving updates.

X2Engine provides a means to alter classes and templates without modifying the original source files. You can create an alternate version of any PHP file, while preserving the original, which makes it much easier to keep track of your changes as well as preserving your alterations through updates. You may still need to rework your custom code when an update is incompatible, but it will be much easier.

Overriding Files

right Practically any php file used in X2Engine can be overridden. X2Engine's customization framework resides in the /custom directory, which mirrors the structure of the source code. For example, /custom/protected/components corresponds to /protected/components. Any php file placed in /custom will be used instead of the original.

Extending Controllers

X2Engine is built on Yii, which uses MVC (model-view-controller) architecture. Overriding an entire file is practical when dealing with views or smaller classes (such as models), but controllers can present a greater challenge. It's only a matter of time before we update one of the 1500+ lines of code in a controller. This means you have to manually find and transfer your changes from the old version, which is time-consuming. To remedy this, we allow substitution of any controller in X2Engine with an extended class.

Whenever a controller is loaded, X2Engine checks for the same filename with "My" at the beginning. For example, if you want to override a single action to ContactsController, you can create a file called MyContactsController in /custom/protected/modules/contacts/controllers containing the following:

<?php
class MyContactsController extends ContactsController {
    function actionIndex() {
        echo 'Hello World!';
    }
}
?>

This file will automatically be used instead of ContactsController and should still work if ContactsController is changed in an update. You may still need to manually merge changes if an update alters the same part of the controller that you changed, but it will be much easier to find.

Custom App Configuration

The configuration for the Yii application can also be customized through the files "web.php", "console.php" and "test.php" in custom/protected/config. These files will be run in the default configuration files for X2Engine running as a web application, console application or unit testing environment (respectively).

An array which mirrors the modified parts of main.php (the default configuration file) should be returned within the custom config file. To customize the configuration of the application, i.e. to add parameters to Yii::app()->params, one only need add/remove/alter the values and keys within $config.

Known Issues

  • Currently, only PHP files can be substituted. To change a CSS or Javascript file you would have to edit the original file.
  • There is no guarantee a customized installation will still work after an update. You will still have to manually merge changes if you want (or need) to use an updated version of a customized file. Depending on the file, you may have to do this for most automatic updates (since some files are changed far more often than others). Luckilly, most of our changes are usually in controller files, which can be extended rather than just replaced, so the updates are less likely to be incompatible with your modifications.