桂电在线-php-提取菜单到配置文件

  1. 新建存储菜单的配置文件 menus.php,并配置菜单
    <?php  if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);
    /*菜单配置*/
    $config[‘menus‘] = array();
    // 校园生活
    $config[‘menus‘][‘life‘] = array(‘name‘ => ‘学校生活‘, 
        ‘submenu‘ => array(
            ‘news/official_news‘ => array(‘name‘ => ‘官网公告‘),
            ‘life/game‘ => array(‘name‘ => ‘校园活动‘, ‘link‘ => ‘life/game‘),
            ‘life/flea‘ => array(‘name‘ => ‘跳蚤专场‘, ‘link‘ => ‘life/flea‘),
            ‘life/calendar‘ => array(‘name‘ => ‘跳蚤专场‘, ‘link‘ => ‘life/calendar‘),
            ‘life/map‘ => array(‘name‘ => ‘校园地图‘, ‘link‘ => ‘life/map‘),
            ‘life/scenery‘ => array(‘name‘ => ‘校园美景‘, ‘link‘ => ‘life/scenery‘),
            ‘life/schoolcard_lose‘ => array(‘name‘ => ‘一卡通丢失‘, ‘link‘ => ‘life/schoolcard_lose‘),
            ‘life/schoolcard_found‘ => array(‘name‘ => ‘一卡通招领‘, ‘link‘ => ‘life/schoolcard_found‘),
        )
    );
    // 校园学习
    $config[‘menus‘][‘study‘] = array(‘name‘ => ‘学校生活‘, 
        ‘submenu‘ => array(
            ‘study/translate‘ => array(‘name‘ => ‘翻译一下‘, ‘link‘ => ‘study/translate‘),
            ‘study/source‘ => array(‘name‘ => ‘分享资源‘, ‘link‘ => ‘study/source‘),
            ‘study/method‘ => array(‘name‘ => ‘小谈学习‘, ‘link‘ => ‘study/method‘),
            ‘study/cet‘ => array(‘name‘ => ‘查询四六级成绩‘, ‘link‘ => ‘study/cet‘),
        )
    );
    // 校园名片
    $config[‘menus‘][‘card‘] = array(‘name‘ => ‘校园名片‘, ‘link‘ => ‘card/index‘
    );
  2. 新建菜单类,Menu.php,并创建获取菜单配置数据的方法(貌似有点多余)/优化方法,修改为读取配置类,封装获取配置文件
    <?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘); 
    class Menu{
    
        /**
         * 获取菜单配置
         */
        public function get_menus($param) {
            $CI =& get_instance();
            $CI->config->load(‘menus‘);
            $menus = $CI->config->item(‘menus‘);
            return $menus;
        }
    }
  3. 控制器index.php 调用菜单类,获取菜单配置数据,赋值模板
    /**
         * 首页
         */
        function index() {
            //引入menu类,获取菜单配置
            $this->load->library(‘menu‘);
            $data[‘menus‘] = $this->menu->get_menus();
            $this->load->view(‘index.html‘, $data);
        }
  4. 模板循环输出菜单
    <ul class="nav navbar-nav navbar-left">
                        <li class="active">
                            <a href="/"> <i class="glyphicon glyphicon-home"></i>
                            </a>
                        </li>
                        <?php foreach ($menus as $menu): ?>
    
                            <?php if ( ! isset($menu[‘submenu‘]) ): ?>
                                <li>
                                    <a href="<?=site_url($menu[‘link‘])?>"><?=$menu[‘name‘]?></a>
                                </li>
                            <?php else: ?>
                                <li>
                                    <a href="" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                        <?=$menu[‘name‘]?>
                                        <span class="caret"></span>
                                    </a>
                                    <ul class="dropdown-menu" role="menu">
                                        <?php foreach ($menu[‘submenu‘] as $link => $submenu): ?>
                                        <li>
                                            <a href="<?=site_url($submenu[‘link‘])?>"><?=$submenu[‘name‘]?></a>
                                        </li>                                
                                        <?php endforeach ?>
                                    </ul>
                                </li>                            
                            <?php endif ?>
    
                        <?php endforeach ?>
                    </ul>

主页菜单部分没完成,困了。明天再配置

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。