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

 

如果你开始使用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);
    }

}

 

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

转载于:https://my.oschina.net/u/2486541/blog/917016

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值