execut/yii2-base My base classes for yii2

yii2-base

My base classes for yii2

Bootstrap system

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 и саму настройку модуля 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'
                ],
            ],
        ],
    ],
],
...

Widgets system

execut\yii\jui\Widget

This class provides the ability to simplify create jquery widgets with assets files if you want. To create a widget, you must create files in follow structure:

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

Create class for you widget:

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

Define asset bundle class in same folder and name with postfix Asset

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

If you want javascript functional, create jquery widget file 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

If you want css styles, create file assets\CustomWidget.css:

.custom-widget {
}

Changelog

Yii2 base classes

1.14.0

  • Added src folder support

1.0.0 - 1.13.4 Changelog was not logged

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

Comments



1.14.2 is the latest of 39 releases



Apache-2.0 license
Stats
1 github stars & 4 github forks
84 downloads in the last day
2318 downloads in the last 30 days
130407 total downloads