如果你开始使用yii超过一个月了,你可能有点体会了。各种快速。各种方便。但是这才是刚刚开始。
现在就来为了更好的使用yii,来点基础知识。进阶必备。
主要就一条:看下基础类的源码,object类,yii的创建object方法。魔术方法的重写。保证全看懂。
进阶实践,学习思路:
1.你该如何通过一个配置。生成一个对象。传递当前对象。
2.如何组件化。简单的数据模型配置,构建简单的增删改查。例如做一个:字段的组件化,视图的公共方法的操作
3.组件化后的扩展。在已经成组件后还不能完成你的需求,如何扩展。
------
关键:
方法;
call_user_func();
\Yii::createObject
关键字:
static
view层的option你会使用吗?
------
贡献个代码
使用联动;
<?= $form->field($model, 'islimited')->widget(SwitchShow::className(), ['willInfluAttr' => ['startexam'=>['1'],'endexam'=>['1']],'dropDownListItems' => ['0'=>'不开启', '1'=>'开启'], 'dropDownListOptions' => []]);?>
<?= $form->field($model, 'startexam')->widget(DateTimePicker::className()) ?>
联动基础类
<?php
namespace source\core\widgets\form;
use yii\helpers\Html;
use yii\widgets\InputWidget;
class SwitchShow extends InputWidget
{
//is array ,key is the will change attribute ,value is array,like ['1'], in it will show
public $willInfluAttr = [];
public $dropDownListItems = [];
public $dropDownListOptions = [];
/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
// $this->model->formName();
$theAttr = $this->attribute;
$htmlFormFieldId = strtolower($this->model->formName() . '-' . $this->attribute);
$needHide = '';
$html = '';
if (!empty($this->willInfluAttr)) {
foreach ($this->willInfluAttr as $key => $value) {
$influFieldKey = strtolower($this->model->formName() . '-' . $key);
$showArray = json_encode($value);
$html .= "if($.inArray($(this).val(),{$showArray}) > -1 ){
$('.field-{$influFieldKey}').show();
}else{
$('.field-{$influFieldKey}').hide();
}";
if (!in_array($this->model->$theAttr, $value)) {
$needHide .= "$('.field-{$influFieldKey}').hide();";
}
}
}
$jsstr = <<<html
$('#{$htmlFormFieldId}').change(function(){
{$html}
});
{$needHide}
html;
$this->view->registerJs($jsstr);
}
/**
* @return string
*/
public function run()
{
$content = [];
$content[] = $this->dropDownList($this->dropDownListItems, $this->dropDownListOptions);
return implode("\n", $content);
}
public function dropDownList($items, $options = [], $generateDefault = true)
{
if ($generateDefault === true && !isset($options['prompt'])) {
$options['prompt'] = '请选择';
}
Html::addCssClass($options, 'form-control');
return Html::activeDropDownList($this->model, $this->attribute, $items, $options);
}
}
计划点:前后端的静态分离后,文件如何放置。资源发布是不是要抛弃。