• PbootCMS二开指南


    最近再用PbootCMS做一些二次开发,开发涉及到【菜单】【权限】等等,做一个简单的记录,方便后期快速理清开发思路。

    一、改为Mysql

    对于PbootCMS用来二次开发,就不要用sqlite来开发了。

    修改文件:config/database.php

    二、开放系统菜单

    默认的系统是没有【系统菜单】功能的,需要改数据库。

    修改数据库:ay_menu 数据表,系统菜单的状态改为:1

    重新登录系统:

    三、图标的使用

    PbootCMS使用的是 fontawesome 字体图标库,该图标库收录了675个图标。

    http://www.fontawesome.com.cn/faicons/

    四、新建目录

    例如我们新建一个【续费提醒】模块:

    再次增加一个【续费客户】菜单:

    重新登录后,就可以看到侧栏菜单。

    五、新建控制器

    默认情况,我们新建模块,需要在 apps/admin/controller 目录下新建控制器

    例如:RenewalController.php

    配置好命名空间后即可访问。

    但是我们看上面的配置都是分模块的,例如:content,member,system,这个在哪里配置呢?

    分模块配置,需要单独的配置路由:

    位置:apps / common / route.php

    六、get 函数

    在使用PbootCMS进行二次开发的时候,经常会看到使用 get 函数,例如:

    $id = get('id', 'int');
    $mcode = get('mcode', 'var');
    $keywords = get('keywords', 'vars');

    通过该 get 函数,可以用于接收参数,并且制定类型,具体函数类型:core / function / helper.php

    然后是这个 filter 方法:

    function filter($varname, $condition)
    {
        // 变量名称文本
        if (array_key_exists($varname, $condition) && $condition[$varname]) {
            $vartext = $condition[$varname];
        } else {
            $vartext = $varname;
        }    
        // 数据源
        if (array_key_exists('d_source', $condition)) {
            switch ($condition['d_source']) {
                case 'post':
                    $data = @$_POST[$varname];
                    break;
                case 'get':
                    $data = @$_GET[$varname];
                    break;
                case 'cookie':
                    $data = @$_COOKIE[$varname];
                    break;
                case 'session':
                    $data = session($varname);
                    break;
                case 'both':
                    $data = @$_POST[$varname] ?: @$_GET[$varname];
                    break;
                case 'string':
                    $data = $varname;
                default:
                    error($vartext . '数据获取方式设置错误!');
            }
            // 去空格
            if (is_string($data))
                $data = trim($data);
        } else {
            $data = $varname; // 没有数据源指定时直接按照字符串过滤处理
        }    
        // 数据为空时,进行是否允许空检测
        if (! $data && array_key_exists('d_none', $condition) && $condition['d_none'] === false) {
            error($vartext . '不能为空!');
        }    
        // 判断是否强制检测,为true时,意味着如果数据不满足要求直接报错,否则返回null
        if (array_key_exists('d_require', $condition) && $condition['d_require'] == true) {
            $require = true;
        } else {
            $require = false;
        }    
        // 数据类型检测
        if (array_key_exists('d_type', $condition)) {
            switch ($condition['d_type']) {
                case 'int':
                    if (! preg_match('/^[0-9]+$/', $data)) {
                        $err = '必须为整数!';
                    }
                    break;
                case 'float':
                    if (! is_float($data)) {
                        $err = '必须为浮点数!';
                    }
                    break;
                case 'num':
                    if (! is_numeric($data)) {
                        $err = '必须为数字!';
                    }
                    break;
                case 'letter':
                    if (! preg_match('/^[a-zA-Z]+$/', $data)) {
                        $err = '只能包含字母!';
                    }
                    break;
                case 'var':
                    if (! preg_match('/^[\w\-\.]+$/', $data)) {
                        $err = '只能包含字母、数字、划线、点!';
                    }
                    break;
                case 'bool':
                    if (! is_bool($data)) {
                        $err = '必须为布尔类型!';
                    }
                    break;
                case 'date':
                    if (! strtotime($data)) {
                        $err = '必须为日期类型!';
                    }
                    break;
                case 'array':
                    if (! is_array($data)) {
                        $err = '必须为数组类型!';
                    }
                    break;
                case 'object':
                    if (! is_object($data)) {
                        $err = '必须为对象类型!';
                    }
                    break;
                case 'vars':
                    if (! preg_match('/^[\x{4e00}-\x{9fa5}\w\-\.,\s]+$/u', $data)) {
                        $err = '只能包含中文、字母、数字、横线、点、逗号、空格!';
                    }
                    break;
                default:
                    if ($condition['d_type'])
                        error($vartext . '数据类型设置错误!');
            }
        }    
        // 非必须或必须但无错误时执行
        if ((! $require || ($require && ! isset($err)))) {        
            // 正则匹配
            if (array_key_exists('d_regular', $condition)) {
                if (! preg_match($condition['d_regular'], $data)) {
                    $err = '不符合正则表达式规则!';
                }
            }
            // 最大值匹配
            if (array_key_exists('d_max', $condition)) {
                if (is_numeric($data)) {
                    if ($data > $condition['d_max']) {
                        $err = '不能大于' . $condition['d_max'];
                    }
                } else {
                    if (mb_strlen($data) > $condition['d_max']) {
                        $err = '长度不能大于' . $condition['d_max'];
                    }
                }
            }
            // 最小值匹配
            if (array_key_exists('d_min', $condition)) {
                if (is_numeric($data)) {
                    if ($data < $condition['d_min']) {
                        $err = '不能小于' . $condition['d_min'];
                    }
                } else {
                    if (mb_strlen($data) < $condition['d_min']) {
                        $err = '长度不能小于' . $condition['d_min'];
                    }
                }
            }
        }    
        // 如果为必须且有错误,则显示错误,如果非必须,但有错误,则设置数据为null
        if ($require && isset($err)) {
            error($vartext . $err);
        } elseif (isset($err)) {
            $data = null;
        }    
        // 如果设置有默认值,默认值
        if (array_key_exists('d_default', $condition)) {
            $data = (! is_null($data)) ? $data : $condition['d_default'];
        }    
        if (is_string($data)) {
            $data = trim($data); // 去空格
            $data = preg_replace_r('/(x3c)|(x3e)/', '', $data); // 去十六进制括号
            $data = preg_replace_r('/pboot:if/i', 'pboot@if', $data); // 过滤插入cms条件语句
            $data = preg_replace_r('/pboot:sql/i', 'pboot@sql', $data); // 过滤插入cms条件语句
            $data = preg_replace_r('/GET\[/i', 'GET@[', $data);
            $data = preg_replace_r('/POST\[/i', 'POST@[', $data);
        }    
        // 销毁错误
        unset($err);    
        // 返回收据
        return escape_string($data);
    }

    到此,基本上就可以用PbootCMS做二次开发了。

  • 相关阅读:
    python lambda
    Java使用Graphics2D生成公章
    纯前端Html+JavaScript+canvas生成公章
    MySQL 大表优化方案,收藏了细看!
    【全干货】5分钟带你看懂 Docker!
    MySQL 分页优化中的 “ INNER JOIN方式优化分页算法 ” 到底在什么情况下会生效?...
    React-Native iOS真机调试(新版)
    React-Native iOS真机调试(新版)
    React-Native iOS真机调试(新版)
    React-Native iOS真机调试(新版)
  • 原文地址:https://www.cnblogs.com/e0yu/p/16147746.html
Copyright © 2020-2023  润新知