Yii 1.1: Extension Development (beginner)

38 followers

To extend the functionality of you web application relative to your requirements you are supposed to use existing yii core libraries or use external libraries. There are some steps to ensure security, uniqueness, modularity, performance and to avoid rework in future.

Step 01: Have a Strategy

  • Is my extension just for fun/demonstration purposes or for everyday use in the real world?
  • Am I writing it to contribute to the community, to promote myself or to earn income?
  • Can I afford the time to offer support to users?
  • Have similar extensions already been written? check here

These questions are important because they impact on how seriously you need to take issues such as coding standards, updates, security, support and documentation. Even if you’re just writing a extension for your own use or for the use of your colleagues, you need to think hard about how you’re going to go about it so that you don’t make a rod for your own back.

Step 02: Use Consistent and Clear Coding Standards

Simple things like consistent spacing, indenting, informative variable naming and succinct comments are a good place to start. If you code to a standard your code will be easier to understand, edit , debug and extends for others in programming world.

Step 03: Development & Use

You can start from scratch or modifying existing one, by modification you can simply extend its functionality what you want. Here we are assuming from scratch !. Before the development classify your extension and try to avoid dependencies on external scripts because its difficult to keep your extension according to external scripts modifications even on other extensions as well for both developer and you, because sometime developer changes extension folder or project file structure.

1) Name extension folder in lower case , dash separate two words,etc like myext, my-ext, myExt,myext-v1.0 etc. 2) Copy all sources/files to folder like js, css, images, supporting libs,classes,namespaces etc. 3) Some comments on top of each or at least one file for extension description, author,contact info, version, package and copyrights license etc.

/**
 * MyCurrentClassName class file.
 * This is description of MyCurrentClassName for developer to use it further !
 * @author Fazal  Burhan <Skype: fburhan810>
 *  @link     http://www.yiiframework.com/user/62626/
 * @copyright Copyright &copy; Fazal Burhan 2014-
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */

4) For Access all the dependent files, use Yii standards Classes like - Register your script - Current extension folder : $dir = dirname(FILE) . DIRECTORY_SEPARATOR . 'sources/files'; - CAssetManager :to avoid naming conflict , available as Yii::app()-assetManager read more

5) Initialize & check all the variables/attribute before use.

6) To access yii core libraries or other classes you can use import() in our extension

Yii::import(zyx.abc.MyClasss); // class
Yii::import('application.components.* ');  // for entire package

7) Use your extensions : if your extension is in protected/extensions the you can access it by

$this->widget('ext.myext.MyExt');

You can pass parameters like

$this->widget('ext.myext.MyExt',
‘property’=>’value’,
       ‘Options’=>array('property1'=>'value1',
            'property2'=>'value2'
),
);

Where ext is alias used for extensions in protected if want to define your own , you can use Yii::setPathOfAlias('myAliase',DIRECTORY_SEPARATOR . 'vendor'); in config

Then

$this->widget('myAliase.myext.MyExt');

Example

<?php
 
/**
 * MyCurrentClassName class file.
 * This is description of MyCurrentClassName for developer to use it further !
 * PHP Version 5.1
 * @package  Widget
 * @author   Fazal Burhan <Skype: fburhan810>
 * @copyright Copyright &copy; Fazal Burhan 2014-
 * @license  http://www.opensource.org/licenses/mit-license.php MIT
 * @link     http://www.yiiframework.com/user/62626/
 */
class MyExt extends CWidget {
 
    /**
     * @var string the hello  .
     */
    public $hello = 'Hello World';
    public $cssFile;  // it better to have private attribute for security then you have to define getter and setter methods
 
    // initialized all the variable and scripts used
 
    public function init() {
 
        // get & publish assets
        $path = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.myext.source', -1, false));
        $this->cssFile = $path . '/myext.css'; // load css
        // register scripts
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile($this->cssFile);    // register css
    }
 
    /**
     * Run this widget.
     * renders the needed HTML code.
     */
    public function run() {
        echo $hello;
    }
 
}
?>
 
<?php
 
$this->widget('ext.myext.MyExt');
//using param 
$this->widget('ext.myext.MyExt', array(
    'hello' => 'Hello world ! i am Here',
));
?>

Step 04: Take Security & Performance Seriously

Security and Performance are basic objectives of yii. There are some interesting topics on it go through according to your requirements if it has high values to you !.

Rule 05: Internationalization

Internationalization doesn’t have to be a big deal but is essential if you want to reach the widest audience possible.

Other

Turn on debug The Yii debugger will find errors in your code that while they may not crash you extension should still be corrected to make sure that you’re doing things correctly. So, turn on YII_DEBUG set true in index.php in root directory.

Study the Yii Core library Yii often has functions that will solve a programming task in one function/class that you may have written many lines of code to achieve. Check out to make sure you’re not writing more code than you need to.

Get some peer review Getting others to look at your code is a strong form of quality control. Other users will find faults that you haven’t noticed in your own testing. Don’t be afraid to post your code: everyone makes mistakes.

Don’t use deprecated functions!

Use a good Yii discussion forum If you have a problem, ask others. There are plenty of forums for Yii solutions

Take some pride in your work! It’s easy to release a Yii with a rider that you take no responsibility for the effects of your code. Now, that’s a necessary given: you can’t be responsible for how your extension will be used. But you can take a professional pride in how you go about your work. Always do your best. Your reputation will be the better for it.

Total 2 comments

#16195 report it
PrplHaz4 at 2014/01/27 07:46am
Use github and IRC to help with peer reviews

Posting your code on github and asking for a review on the yii IRC channel is a good way to get others to look at your code.

#16134 report it
trond at 2014/01/22 01:14pm
Great tutorial!

It also worth mentioning that if you use a good IDE (like PhpStorm) you get valuable help with formatting the code properly, detecting code weaknesses and injecting standard documentation blocks.

Leave a comment

Please to leave your comment.

Write new article