unyii2 / yii2-panel
yii2 面板模块
1.0.2
2021-10-16 17:54 UTC
Requires
- php: 7.*
This package is auto-updated.
Last update: 2024-08-24 16:34:51 UTC
README
Yii2Panel旨在创建可以显示不同模块/扩展不同面板的权限控制面板。
正如预期的那样,表现良好,因为Yii2Panel可以用于显示任何其他模块/扩展的权限控制面板。
另一个好处是,要显示的面板被分配到一个模块配置,这使得在不同的项目中可以使用不同的面板。
从PanelWidget处理提交的数据可以使用PanelLogic
实现
简单的仪表盘解决方案。每个仪表盘面板都定义为与Yii页面相同的面板控制器操作
- 在行为中的面板控制器可以控制访问 - 仅对有访问权限的用户显示面板
- 面板控制器控制器操作用于创建HTML或响应
- 在所有模块控制器视图相同的文件夹中创建视图文件夹
- 在视文件中显示添加PanelWidget,就像锚点一样
- 在模块配置中为PanelWidget设置一个或多个面板作为Yii路由,带有参数到面板控制器
顺序
- PanelWidget从模块配置中获取面板列表
- PanelWidget调用带有参数的面板控制器操作
- 面板控制器验证访问权限。如果没有访问权限,则返回空字符串
- 面板控制器操作创建响应HTML
- PanelWidget输出响应HTML
通过composer安装
{
"require": {
"unyii2/yii2-panel": "dev-master"
}
}
Or
$ composer require unyii2/yii2-panel "dev-master"
小部件
echo \unyii2\yii2panel\PanelWidget::widget([ 'name' => 'exportSettings', 'params' => [ 'docId' => 777 ] ]);
控制器操作
对于处理来自面板小部件的提交数据,可以使用PanelLogic
public function actionCreate() { $model = new RkInvoiceForm(); if($model->load($request->post()) && $model->save()) { $panelLogic = Yii::createObject([ 'class' => PanelLogic::class, 'name' => 'LietvedibaRkInvoiceSave', 'params' => [ 'modelRecordId' => $model->id ] ]); $panelLogic->run(); return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', [ 'model' => $model, ]); }
模块配置
向模块添加参数'panels',并在模块配置中添加面板路由
'invoices' => [ 'class' => 'd3modules\d3invoices\Module', 'panels' => [ /** for widget */ 'exportSettings' => [ [ 'route' => 'd3accexport/invoice-panel/document-settings', 'params' => [ 'docId' => 13 // action parameter value ] 'tag' => 'div', // optinal. Add enclosing tag to panel 'options' => ['class' => 'col-sm-8 col-md-6 col-lg-4'] //enclosing tag options ] ], /** for panel logic */ 'LietvedibaRkInvoiceSave' => [ [ 'route' => 'deal/panel/save' ] ], ], ],
向控制器添加参数'panels',并在模块配置中添加面板路由
'invoices' => [ 'class' => 'd3modules\d3invoices\Module', 'controllerMap' => [ 'settings' => [ 'class' => 'yii3\persons\controllers\SettingsController', 'panels' => [ 'UserSettingsProvileLeft' => [ [ 'route' => 'myauth/panel/show-qrc' ] ] ] ], ],
如果无法将模块参数'panels'添加到模块中,可以在参数中定义面板路由
'params' => [ 'panelWidget' => [ 'dashboard' => [ 'last' => [ [ 'route' => 'delivery/panel/transit-declarations', /** * parameters for action method: * public function actionMyTransitDeclarations(array $statusIdList): string */ 'params' => [ 'statusIdList' => [5, 20] ] ] ], ] ], ]
带有访问控制和视图渲染的面板控制器
标准视图路径:d3modules/d3accexport/views/invoice-panel/setting_grid.php
<?php namespace d3modules\d3accexport\controllers; use unyii2\yii2panel\Controller; use yii\filters\AccessControl; class InvoicePanelController extends Controller { /** * @inheritdoc */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ /** * standard definition */ [ 'allow' => true, 'actions' => [ 'document-settings', ], 'roles' => [ 'DocSetting', ], ], /** * roles define in panel module config. * Example of edi module config: * 'edi' => [ * 'class' => 'd3yii2\d3edi\Module', * 'accessRulesMessageRoles' => ['Depo3EdiFull'] * ], * In Module add property: * class Module extends D3Module * public $accessRulesMessageRoles; * .... */ [ 'allow' => true, 'actions' => [ 'message', ], 'roles' => $this->module->rulesMessageRoles??['@'], ], ], ], ]; } /** for widget */ public function actionDocumentSettings() { return $this->render('setting_grid',[]); } /** for widget */ public function actionMessage() { return $this->render('message',[]); } /** for controller logic */ public function actionSave(int $modelRecordId): bool { if (!$model = DealInvoice::findOne(['invoice_id' => $modelRecordId])) { $model = new DealInvoice(); $model->invoice_id = $modelRecordId; } $request = Yii::$app->request; if ($model->load($request->post()) && $model->deal_id && $model->save() ) { return true; } if ($model->hasErrors()) { throw new \Exception(json_encode($model->getErrors())); } return true; } }