execut/yii2-base

我为 yii2 定制的基类

安装次数: 143,436

依赖者: 18

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 4

开放性问题: 0

类型:yii2-extension

1.14.2 2020-11-18 07:16 UTC

README

我为 yii2 定制的基类

引导系统

execut\yii\Bootstrap

此类是允许按层次启动不同模块的组件。例如,存在一个名为 users 的模块,它为了工作需要 execut/yii2-navigation 导航和 CRUD 模块 execut/yii2-crud。它们需要在启动 users 模块之前启动。同时,这些组件自身还需要启动其他组件来工作。例如,execut/yii2-crud 需要包 execut/yii2-actions。使用 execut\yii\Bootstrap 组件可以递归地自动获取所有必要的组件,无需担心这些细节。我们只需要启动 users 模块,并指定它依赖于哪些组件。为此,需要实现 \execut\yii\Bootstrap 类的子类,并在其中声明 users 模块的依赖和配置

<?php
namespace execut\users;


class Bootstrap extends \execut\yii\Bootstrap
{
    public function getDefaultDepends()
    {
        return [
            'bootstrap' => [
                'yii2-navigation' => [
                    'class' => \execut\navigation\Bootstrap::class,
                ],
                'yii2-crud' => [
                    'class' => \execut\crud\Bootstrap::class,
                ],
            ],
            'modules' => [
                'users' => [
                    'class' => Module::class,
                ],
            ],
        ];
    }
    
    public function bootstrap($app)
    {
        parent::bootstrap($app); // Здесь можно задать код для бутстрапа модуля. Родителя вызвать обязательно
    }
}

之后,在应用程序配置中添加 users 模块的启动。引导系统的美妙之处在于,不需要在配置中指定模块及其所需的所有组件,因为这些都在引导系统中声明了。

...
'bootstrap' => [
    'users' => [
        'class' => \execut\users\Bootsrap::class,
    ],
],
...

如果还需要为模块设置应用程序环境参数,则可以通过配置文件中的 depends 指令来指定。

...
'bootstrap' => [
    'users' => [
        'class' => \execut\users\Bootsrap::class,
        'depends' => [
            'modules' => [
                'users' => [
                    'defaultRoute' => '/users'
                ],
            ],
        ],
    ],
],
...

小部件系统

execut\yii\jui\Widget

此类提供了简化创建带有资源文件的 jQuery 小部件的能力。要创建小部件,必须创建以下结构的文件

  • CustomWidget.php
  • CustomWidgetAsset.php
  • assets\CustomWidget.js
  • assets\CustomWidget.css
  1. CustomWidget.php

为你的小部件创建一个类

class CustomWidget extends \execut\yii\jui\Widget
{
    public function run()
    {
        /**
         * If you want append assets files and create javascript widget instance
         */
        $this->registerWidget();
        
        /**
         * Or if you want only append assets files
         */
        $this->_registerBundle();

        /**
         * renderContainer - helper method for wrapping widget in div container with defined in widget options
         */
        $result = $this->renderContainer($this->renderWidget());

        return $result;
    }
    
    protected function renderWidget() {
        /**
         * Here generate widget out $result
         */
         
         return $result;
    }
}
  1. CustomWidgetAsset.php

在同一文件夹中定义与文件同名的资源包类,并添加后缀 Asset

class CustomWidgetAsset extends \execut\yii\web\AssetBundle
{
}
  1. assets\CustomWidget.js

如果你需要 JavaScript 功能,则创建 assets\CustomWidget.js 文件

(function () {
    $.widget("customNamespace.CustomWidget", {
        _create: function () {
            var t = this;
            t._initElements();
            t._initEvents();
        },
        _initElements: function () {
            var t = this,
                el = t.element,
                opts = t.options;
        },
        _initEvents: function () {
            var t = this,
                el = t.element,
                opts = t.options;
        }
    });
}());
  1. assets\CustomWidget.css

如果你需要 CSS 样式,则创建 assets\CustomWidget.css 文件

.custom-widget {
}