Yii 1.1: YiiFunctions - Helper file, a collections of Yii function to make the Yii coding shorten



Helper file, a collections of Yii function to make the Yii coding shorten.

The goal of this file is to make more simplication and new way of coding design.

  • How to install this file ?
    1 ) For you to able to organize the location of the directory, create a subfolder under the 'protected' folder name 'functions'

    2 ) On the index.php, insert the line

require_once(dirname(__FILE__) . '/protected/functions/yii.php');
// before the line.


// change the following paths if necessary
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once(dirname(__FILE__) . '/protected/functions/yii.php'); // I insert this new line for my index.php file

3 ) That's all, you're now finish installing the functions.

  • How to use ?
    Benefits below:
// Before
// Now
// Before
// Now
// Before
CHtml::link($text, $url, $htmlOptions);
// Now 
hyperlink($text, $url, $htmlOptions)

You may download it here : https://github.com/ersongit/YiiFunctions

Total 3 comments

#10564 report it
pmaselkowski at 2012/11/05 06:34am
params function

For params you could use something like that:

 * Access array elements via dot notation
 * @param string $path
 * @param mixed[] $arr
 * @return mixed
function dotToArray($path, &$arr)
    $parts = explode('.', $path);
    $path = & $arr;
    foreach($parts as $e)
        $path = & $path[$e];
    return $path;

I use shortcut functions mostly in views, here are only ones I use:

  • app() - instance of application
  • user() - returns Yii::app()->user->model, not Yii::app()->user
  • tx() - for translations, with different params order, as most of the time I don't use $category
  • msnippet() - function to get common configuration snippets, for grids, buttons etc.

Here is msnippet() if anyone interested:

 * Get frequently used snippet.
 * snippetId should start with module name, or app for main snippets.
 * If started with dot '.' uses current module
 * @example app.some.config.value, content.editor.toolbar.lists, .editor.toolbar.lists
 * @staticvar mixed[] $snippets
 * @param string $snippetId
 * @return mixed
function msnippet($snippetId)
    static $snippets = [];
    $parts = explode('.', $snippetId);
    $module = array_shift($parts);
        $module = Yii::app()->controller->module->id;
        $snippetId = sprintf('%s%s', $module, $snippetId);
        if($module == 'app')
            $snippets[$module] = require sprintf('%s/../config/snippets.cfg.php', dirname(__FILE__), $module);
            $snippets[$module] = require sprintf('%s/%s/config/snippets.cfg.php', Yii::app()->modulePath, $module);
    return dotToArray($snippetId, $snippets);
#10543 report it
Boaz at 2012/11/03 05:38pm
I recommend using a decent IDE

with 'templates' feature. In my case, I had my IDE convert 'user' to 'Yii::app()->user->' with the flick of a tab key press (just to give an example. I use several of these for commonly used constructs). I second the reasons sounded by Jmper against the approach demonstrated in this article.

#10540 report it
Jmper at 2012/11/03 10:58am

I used similar helper file in one of my first yii projects. Soon I discovered it brings more trouble than advantages.
First, you have to learn another layer of functions that actually bring nothing new to your application, just make the code shorter (well, not always - how shorter it is to write webapp() instead of Yii::app()? Exactly 2 characters).
Next, you have to remember for which functions you have shortcuts, and can use them, and for which not. I found myself easily forgetting and sometimes using shortcuts, sometimes the originals, causing a bit of mess in the code. Finally, for others who view and try to comprehend your code, and who know nothing about your convention, the code becomes harder to understand; they have to waste time before they discover the new functions and learn what they are for.
Perhaps the trick with parameters is interesting and worth implementing (this is the only part that brings something new and innovative); but only if you really have a lot of complex params in your project and use them often.

Leave a comment

Please to leave your comment.

Write new article