weiphp基础插件控制器

<?php

namespace Home\Controller;      //命名空间

use Think\Controller;       //引用系统控制器

/**
 * 扩展控制器
 * 用于调度各个扩展的URL访问需求
 */
class AddonsController extends Controller {     //插件扩展控制器
    protected $addons = null;       //受保护的字段$addons为空
    protected $addon, $model;       //受保护的字段$addon,$model
    function _initialize() {        //初始化方法
        $this->initFollow ();       // 公众号粉丝信息初始化 
        
        C ( 'EDITOR_UPLOAD.rootPath', './Uploads/Editor/' . get_token () . '/' );       //设置EDITOR_UPLOAD.rootPath值
    }
    public function execute($_addons = null, $_controller = null, $_action = null) {    //定义execute方法
        if (! empty ( $_action ) && ! empty ( $_addons ) && empty ( $_controller )) {   //如果传参全不为空
            $_controller = $_GET ['_controller'] = $_addons;    //设置$_controller、$_addons的值相等且等于传过来的_controller的值
            $_REQUEST ['_controller'] = $_REQUEST ['_addons'];  //全局变量中_controller和_addons相等
        }
        
        if (C ( 'URL_CASE_INSENSITIVE' )) {
            $_addons = ucfirst ( parse_name ( $_addons, 1 ) );  //转换$_addons串风格,并让首字母大写
            $_controller = parse_name ( $_controller, 1 );  //转换$_addons串风格
        }
        
        define ( 'ADDON_PUBLIC_PATH', __ROOT__ . '/Addons/' . $_addons . '/View/default/Public' );  //定义ADDON_PUBLIC_PATH
        defined ( '_ADDONS' ) or define ( '_ADDONS', $_addons );    //判断并定义_ADDONS
        defined ( '_CONTROLLER' ) or define ( '_CONTROLLER', $_controller );       //判断并定义_CONTROLLER
        defined ( '_ACTION' ) or define ( '_ACTION', $_action );    //判断并定义_ACTION
        
        $token = get_token ();    //获取token
        if (in_array ( $_action, array (    //判断$_action是在数组中
                'lists',
                'config',
                'nulldeal' 
        ) ) && (empty ( $token ) || $token == '-1')) {  //$token 为空或者等于-1
            $this->error ( '请先增加公众号!', U ( 'Home/MemberPublic/lists' ) );   //提示并跳转到会员列表页
        }
        
        $this->_nav ();    //调用 _nav()方法
        
        if (! empty ( $_addons ) && ! empty ( $_controller ) && ! empty ( $_action )) { //都不为空
            tongji ( $_addons );    //统计插件
            
            A ( "Addons://{$_addons}/{$_controller}" )->$_action ();    //实例化控制器
        } else {
            $this->error ( '没有指定插件名称,控制器或操作!' );    //报错
        }
    }
    function _nav() {
        $map ['name'] = _ADDONS;    //设置查询条件
        $this->addon = $addon = M ( 'Addons' )->where ( $map )->find ();    //查询Addons表,给本        对象下addon赋值
        $nav = array ();    //准备空数组
        if ($addon ['has_adminlist']) {//判断并组成数组
            $res ['title'] = $addon ['title'];
            $res ['url'] = U ( 'lists' );
            $res ['class'] = _ACTION == 'lists' ? 'current' : '';
            $nav [] = $res;
        }
        if (file_exists ( ONETHINK_ADDON_PATH . _ADDONS . '/config.php' )) {//检查插件目录下confing文件是否存在
            $res ['title'] = '功能配置';
            $res ['url'] = U ( 'config' );
            $res ['class'] = _ACTION == 'config' ? 'current' : '';
            $nav [] = $res;
        }
        if (empty ( $nav ) && _ACTION != 'nulldeal') {  //数组为空且者_ACTION不等于‘nulldeal’
            U ( 'nulldeal', '', true );     /
        }
        $this->assign ( 'nav', $nav );  //交$nav数据给模板
        
        return $nav;    //返回该值
    }

 

posted on 2015-07-10 02:10  dennr  阅读(383)  评论(0编辑  收藏  举报