YiiFunctions
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 ?
Steps:
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. Yii::createWebApplication($config)->run();
Example:
// change the following paths if necessary $yii=dirname(__FILE__).'/1.1.9/framework/yii.php'; $config=dirname(__FILE__).'/1.1.9/protected/config/main.php'; // 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 require_once($yii); Yii::createWebApplication($config)->run();
3 ) That's all, you're now finish installing the functions.
// Before Yii::app()-> // Now webapp()->
// Before Yii::app()->params['params1']['params2']['params3'] // Now params('params1.params2.params3')
// Before CHtml::link($text, $url, $htmlOptions); // Now hyperlink($text, $url, $htmlOptions)
You may download it here : https://github.com/ersongit/YiiFunctions
Total 3 comments
For params you could use something like that:
I use shortcut functions mostly in views, here are only ones I use:
Here is msnippet() if anyone interested:
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.
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 login to leave your comment.