热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php读取数据库json文件路径,ThinkPHP5.1遍历目录获取json文件渲染栏目

公共控制器***公共初始化*Author闻子*Return\think\response\View|void*publicfunctioninit

公共控制器

/**

* 公共初始化

* &#64;Author 闻子 <270988107&#64;qq.com>

* &#64;Return \think\response\View|void

*/

public function initialize()

{

$this->NavBarlist();

}

/**

* 后台侧边栏

* &#64;Author 闻子 <270988107&#64;qq.com>

*/

private function NavBarlist()

{

$modulesList &#61; getModulesCatalog(Env::get(&#39;app_path&#39;));

$labels &#61; [];

$jsonData &#61; [];

foreach ($modulesList as $k&#61;>$v) {

$modulesUrl &#61; Env::get(&#39;app_path&#39;) . $v . &#39;\info.json&#39;;

$jsonData[] &#61; json_decode(file_get_contents($modulesUrl),true);

}

foreach ($jsonData as $key &#61;> $value) {

if (!empty($value[&#39;admin_menu&#39;])) {

$labels[] &#61; $jsonData[$key][&#39;admin_menu&#39;];

}

}

foreach ($labels as $key &#61;> $item) {

foreach($item as $k &#61;> $v){

$arr[] &#61; $v;

}

}

$this->assign(&#39;NavBarlist&#39;,$arr);

}

公共文件common

/**

* 获取模块目录

* &#64;Author 闻子 <270988107&#64;qq.com>

* &#64;Param $dir

* &#64;Return array

*/

function getModulesCatalog($dir)

{

$dirArray[] &#61; NULL;

if (false !&#61; ($handle &#61; opendir($dir))) {

$i &#61; 0;

while (false !&#61;&#61; ($file &#61; readdir($handle))) {

if ($file !&#61; "." && $file !&#61; ".." && $file !&#61; ".htaccess" && $file !&#61; "common" && !strpos($file, ".")) {

$dirArray[$i] &#61; $file;

$i&#43;&#43;;

}

}

closedir($handle);

}

return $dirArray;

}

json文件

{

"name": "admin",

"title": "后台",

"description": "一款基础后台模块",

"author": "Wz",

"version": "1.0.0",

"is_module": 1,

"is_admin": 1,

"admin_menu": [

{

"title": "仪表盘",

"url": "admin/index/index",

"icon": "layui-icon-dashboard",

"is_status": 1

},

{

"title": "模块管理",

"url": "",

"icon": "layui-icon-dashboard",

"is_status": 1,

"menu_list": [

{

"title": "模块列表",

"url": "admin/modules/index",

"is_status": 1

}

]

}

],

"database_prefix": "wz_"

}



推荐阅读
author-avatar
谁许我一世繁华似锦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有