1、定义插件
class MyPlugin extends AbstractPlugin {
private $myPlugin;
public function __construct(PluginManager $pm) {
$this->myPlugin= $pm->getServiceLocator()->get('Translator'); //插件ID
}
//使用参数
public function __invoke($message, $textDomain = 'default', $locale = null) {
return $this->translator->translate($message, $textDomain, $locale);
}
}
--- 注册插件 ----
module.config.php
'controller_plugins'
=>array(
'invokables'=>array(
'abcPlugin' =>Admin\Plugins\MyPlugin,
)
),
3、可以在插件中使用控制器方法
//查询数据库
private functiongetInfoTable()
{
$sm=
$this->getController()->getServiceLocator();
return$sm->get('Admin\Model\InfoTable');
}
4、在Controller调用
$this->abcPlugin('example message');
5、在控制器使用Helper
$helper = $this->getServiceLocator()->get('viewhelpermanager')->get('helperName');
$helper(params1,...);
=============在视图插件中添加方法==============
1.插件类
class InfoHelper extends AbstractHelper
{
protected $infoTable;
/**
* 在.config中注册,注意对应的名称空间,视图中可直接调用$this->xxxx
* $infoWhere可定义$cate_path_id: string; $categoryIds: int/array $categoryIds:指定分类ID
* 适用于视图中调用
* return bool
*/
public function __invoke($infoWhere=[], $limit=null, $order=null)
{
$infoTable = $this->getInfoTable();
$where = array(
'status'=>1,
);
$where = array_merge($where, $infoWhere);
//param: ($where=null, $qs=null, $paginated=false, $limit=null, $order=null)
$resultSet = $infoTable->fetchAll($where, null, false, $limit, $order);
return $resultSet;
}
public function infoPhoto($infoWhere=[], $uploadWhere=[], $limit=null, $order=null)
{
$infoTable = $this->getInfoTable();
$where = array(
'status'=>1,
);
$infoWhere = array_merge($where, $infoWhere);
//param: ($infoWhere=null, $uploadWhere=null, $limits=null, $order=null)
$resultSet = $infoTable->getInfoPhotoRowsFull($infoWhere, $uploadWhere, $limit, $order);
return $resultSet;
}
public function getInfoTable()
{
if (!$this->infoTable){
$this->infoTable = $this->getView()->getHelperPluginManager()->getServiceLocator()->get('Admin\Model\InfoTable');
}
return $this->infoTable;
}
}2、注册插件(.config中)
3、在视图中调用helper方法
$infoPhoto = $this->plugin('infoHelper')->infoPhoto();

被折叠的 条评论
为什么被折叠?



