petrgrishin / yii-widget-factory
Yii 组件工厂
1.0.0
2014-05-23 18:21 UTC
Requires
- php: >=5.3.0
- yiisoft/yii: *
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 16:19:15 UTC
README
Yiiframework 组件工厂
安装
将依赖项添加到您的项目的 composer.json 文件中
{ "require": { "petrgrishin/yii-widget-factory": "dev-master" } }
问题定义
需要使用模块中实现并具有模块依赖的组件,例如对模块控制器(如模块中的“comments/list”控制器)的了解。
解决方案
在模块中实现创建组件工厂并使用其中存储的知识(如组件的参数,例如“listUrl”)初始化它。要在应用程序中使用组件,请使用创建的工厂。
解决方案示例
####模块 类模块。使用(参数组件 listUrl
)控制器 comments/list
的知识(位于当前模块)初始化组件工厂。
use \PetrGrishin\WidgetFactory\WidgetFactory; class CommentsModule extends CWebModule { public function getCommentsWidgetFactory() { if (empty($this->_commentsWidgetFactory)) { $this->_commentsWidgetFactory = new WidgetFactory(); $this->_commentsWidgetFactory ->setClassName(Widgets\CommentsWidget::className()) ->setParams(array( 'listUrl' => $this->createModuleUrlBuilder('comments/list'), )); } return $this->_commentsWidgetFactory; } }
####主应用程序 类控制器
class SiteController extends CController { public function actionDetail() { $this->render('detail', array( 'commentsWidgetFactory' => $this->getCommentsWidgetFactory(), )); } /** * @return \PetrGrishin\WidgetFactory\WidgetFactory */ protected function getCommentsWidgetFactory() { return $this->getCommentsModule()->getCommentsWidgetFactory() ->setView($this); } /** * @return CommentsModule */ protected function getCommentsModule() { return Yii::app()->getModule('comments'); } }
控制器表示和创建其中的组件
$commentsWidget = $commentsWidgetFactory->createInstance(array('param' => 'value')); $commentsWidget->run();