Membuat fungsi yang dipakai semua controller pada yii
Sedikit tips saja bagi yang sering menggunakan yii..Jika kita menggunakan fungsi yang sering diulang - ulang misalnya dalam sebuah datepicker dengan menggunakan CJuiDatePicker pastinya kita akan menuliskan menggunakan dengan seperti ini :
$this->widget('zii.widgets.jui.CJuiDatePicker',
array(
// you must specify name or model/attribute
//'model'=>$model,
//'attribute'=>'projectDateStart',
'name'=>'Project[projectDateStart]',
// optional: what's the initial value/date?
//'value' => $model->projectDateStart
'value' => '08/20/2010',
// optional: change the language
//'language' => 'de',
//'language' => 'fr',
//'language' => 'es',
'language' => 'pt-BR',
/* optional: change visual
* themeUrl: "where the themes for this widget are located?"
* theme: theme name. Note that there must be a folder under themeUrl with the theme name
* cssFile: specifies the css file name under the theme folder. You may specify a
* single filename or an array of filenames
* try http://jqueryui.com/themeroller/
*/
'themeUrl' => Yii::app()->baseUrl.'/css/jui' ,
'theme'=>'pool', //try 'bee' also to see the changes
'cssFile'=>array('jquery-ui.css' /*,anotherfile.css, etc.css*/),
// optional: jquery Datepicker options
'options' => array(
// how to change the input format? see http://docs.jquery.com/UI/Datepicker/formatDate
'dateFormat'=>'mm/dd/yy',
// user will be able to change month and year
'changeMonth' => 'true',
'changeYear' => 'true',
// shows the button panel under the calendar (buttons like "today" and "done")
'showButtonPanel' => 'true',
// this is useful to allow only valid chars in the input field, according to dateFormat
'constrainInput' => 'false',
// speed at which the datepicker appears, time in ms or "slow", "normal" or "fast"
'duration'=>'fast',
// animation effect, see http://docs.jquery.com/UI/Effects
'showAnim' =>'slide',
),
// optional: html options will affect the input element, not the datepicker widget itself
'htmlOptions'=>array(
'style'=>'height:30px;
background:#ffbf00;
color:#00a;
font-weight:bold;
font-size:0.9em;
border: 1px solid #A80;
padding-left: 4px;'
)
)
);
Nah misalnya kita ingin membuat fungsi tersebut dapat dipakai terus tanpa harus menuliskan keseluruhan kode tersebut kita dapat mengakalinya dengan menuliskannya di Components/Controller.php
nah di Controller buat fungsi sebagai berikut :
public function createDatePicker($model,$attribute){
$m=get_class($model);
$t=$m.'['.$attribute.']';//ingat id dan name dari form ternyata formatnya dikasih model[nama_atribute]
$this->widget('zii.widgets.jui.CJuiDatePicker',array(
'name'=>$t,
'model'=>$model,
'value'=>$model->$attribute,
'options'=>array(
'showAnim'=>'fold',
'dateFormat' => 'yy-mm-dd', // save to db format
'themeUrl' => Yii::app()->baseUrl.'/css/jui' ,
'theme'=>'pool', //try 'bee' also to see the changes
'cssFile'=>array('jquery-ui.css' /*,anotherfile.css, etc.css*/),
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
)
);
Nah setelah itu tinggal digunakan di view kita deh tinggal panggil fungsi tersebut daripada harus membuat satu per satu<div class="row"> <?php echo $form->labelEx($model,'end_time_planned'); ?> <?php echo $this->createDatePicker($model,'end_time_planned'); ?> <?php echo $form->error($model,'end_time_planned'); ?> </div>Yup simple kan tinggal gunakan fungsi tersebut di form yang kita inginkan..
Pada dasarnya kita membuat fungsi di class super dari masing - masing controller yang ada di project kita. Karena semua Controller yang kita buat mengextends Controller yang ada di Components/Controller.php. Yap itu mungkin sekedar trik aja..