execut / yii2-base
我为 yii2 定制的基类
1.14.2
2020-11-18 07:16 UTC
Requires
- yiisoft/yii2: >=2.0.13
- yiisoft/yii2-jui: @dev
- dev-master
- 1.14.2
- 1.14.1
- 1.14.0
- 1.13.4
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.4
- 1.10.3
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.0
- 1.0.3.x-dev
- 1.0.2.x-dev
- dev-mvc-test-support
This package is auto-updated.
Last update: 2024-09-18 15:20:44 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
- 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; } }
- CustomWidgetAsset.php
在同一文件夹中定义与文件同名的资源包类,并添加后缀 Asset
class CustomWidgetAsset extends \execut\yii\web\AssetBundle { }
- 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; } }); }());
- assets\CustomWidget.css
如果你需要 CSS 样式,则创建 assets\CustomWidget.css 文件
.custom-widget {
}