欢迎光临
我们一直在努力

thinkphp5 导航选中解析函数

项目中,通常我们要做动态的导航选中事件 如

 

代码如下

if(!function_exists('get_rule_deal')) {
    /**
     * 导航URL解析 返回是否选中状态
     * $rule string 规则 (如:home/index/*?type=2&id=*  参不确定用*标识 )
     * ( module与controller可以默认不写,GET规则根据需求写 如: index?type=1 或 index 即可)
     * @author 爱是西瓜 <ztlong2@qq.com>
     */
    function get_rule_deal($rule)
    {

        if (!trim($rule)) return false;

        $exp = explode('?', $rule);

        //解析url方法
        if ($exp[0]) {
            $ctr = explode('/', $exp[0]);

            $data = [];
            $data['module'] = request()->module();
            $data['controller'] = request()->controller();
            $data['action'] = request()->action();

            if (count($ctr) == 1) {
                $data['action'] = $ctr[0];
            }

            if (count($ctr) == 2) {
                $data['controller'] = $ctr[0];
                $data['action'] = $ctr[1];
            }
            if (count($ctr) == 3) {
                $data['module'] = $ctr[0];
                $data['controller'] = $ctr[1];
                $data['action'] = $ctr[2];
            }

            if ($data['module'] != '*' && strcasecmp($data['module'], request()->module()) != 0) {
                return false;
            }

            if ($data['controller'] != '*' && strcasecmp($data['controller'], request()->controller()) != 0) {
                return false;
            }

            if ($data['action'] != '*' && strcasecmp($data['action'], request()->action()) != 0) {
                return false;
            }

        }

        //解析url参数
        $attr = [];
        if ($exp[1]) {
            $join = explode('&', $exp[1]);

            foreach ($join as $k => $v) {
                list($key, $value) = explode('=', $v);
                $attr[$key] = $value;

                if (input($key) === null) {
                    return false;
                } else if ($value != '*' && strcasecmp(input($key), $value) != 0) {
                    return false;
                }

            }
        }

        return true;

    }
}

实例DEMO

if(!function_exists('get_nav_list')) {
    /**
     * 获取导航列表
     * @author 爱是西瓜 <ztlong2@qq.com>
     */
    function get_nav_list()
    {
        $nav = [
            ['url' => '','rule'=>'loan/*?type=1', 'title' => '信用贷'],
            ['url' => '','rule'=>'loan/*?type=2', 'title' => '车抵贷'],
            ['url' => '','rule'=>'loan/*?type=3', 'title' => '房抵贷'],
            ['url' => '','rule'=>'credit/*', 'title' => '信用卡'],
            ['url' => 'calc/index', 'rule'=>'calc/*','title' => '工具'],
            ['url' => '','rule'=>'need/index', 'title' => '贷款需求'],
        ];
        
        foreach ($nav as $k=>$v){

            $v['active'] = get_rule_deal($v['rule'])?'on':'';

            $v['url'] = url($v['url']);
            $nav[$k] = $v;
        }

        return $nav;
    }
}

未经允许不得转载:mbku » thinkphp5 导航选中解析函数

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

翰林黄金-重庆运营中心

翰林黄金翰林金业