学习YII2 ---是时候进阶下了

 

若是你开始使用yii超过一个月了,你可能有点体会了。各类快速。各类方便。可是这才是刚刚开始。php

如今就来为了更好的使用yii,来点基础知识。进阶必备。html

主要就一条:看下基础类的源码,object类,yii的建立object方法。魔术方法的重写。保证全看懂。json

进阶实践,学习思路:后端

1.你该如何经过一个配置。生成一个对象。传递当前对象。yii

2.如何组件化。简单的数据模型配置,构建简单的增删改查。例如作一个:字段的组件化,视图的公共方法的操做ide

3.组件化后的扩展。在已经成组件后还不能完成你的需求,如何扩展。组件化

------学习

关键:this

方法;spa

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);
    }

}

 

计划点:先后端的静态分离后,文件如何放置。资源发布是否是要抛弃。

相关文章
相关标签/搜索