dongshi9526 2016-01-24 09:41
浏览 20
已采纳

php数组页面系统,无法正常工作

So I'm working on setting up my first "fancy" page system, and I have run into a problem. I use the code seen below, and it loads the "profile" page without any problems, and the default page works fine too. However the other two pages does not show at all, and I can't seem to request them in the URL eighter. All the files are there tho. Any help here will be much appreciated! :)

if(isset($_SESSION['user_id'])){
    require('user.php');
    $player = new user($_SESSION['user_id'], $database);

    $default_page = 'profile';
    $pages = array(
        'profile' => array(
            'name' => 'Profile',
            'file' => 'profile.php',
            'function' => 'profile',
            ),
        'create_monster' => array(
            'name' => 'Create Monster',
            'file' => 'monsterPages.php',
            'function' => 'createMonster',
            ),
        'create_attack' => array(
            'name' => 'Create Attack',
            'file' => 'attackPages.php',
            'function' => 'createAttack',
        ),
    );

    if(!empty($_GET['page'])){
        $page = strtolower(trim($_GET['page']));

        if(isset($pages[$page])){
            require($pages[$page]['file']);
            echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
            $pages[$page]['function']();
    }
    else{
            require($pages[$default_page]['file']);
            echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
            $pages[$default_page]['function']();
    }
}
else{
    require($pages[$default_page]['file']);
    echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
    $pages[$default_page]['function']();    
    }

}
  • 写回答

2条回答 默认 最新

  • douou5933 2016-01-24 11:03
    关注

    You've got some redundancy in your code. You could abbreviate it.

    $page = isset($_GET['page']) ? $_GET['page'] : $default_page;
    
    // You should check that the page exists here.
    
    require($pages[$page]['file']);
    echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
    $pages[$page]['function']();
    

    I suggest you place the page loading code in a function. I've created a simple class here:

    <?php
    
    $pages = array(
        'profile' => array(
            'name' => 'Profile',
            'file' => 'profile.php',
            'function' => 'profile',
            ),
        'create_monster' => array(
            'name' => 'Create Monster',
            'file' => 'monsterPages.php',
            'function' => 'createMonster',
            ),
        'create_attack' => array(
            'name' => 'Create Attack',
            'file' => 'attackPages.php',
            'function' => 'createAttack',
        ),
    );
    
    
    class PageLoader
    {
    
        public $pages;
        public $base_path;
    
        public function __construct($pages, $base_path)
        {
            $this->pages = $pages;
            $this->base_path = $base_path;
        }
    
        public function run($name) {
            if(! isset($this->pages[$name])) {
                throw new Exception('Page not found');
            } 
    
            $path = $this->base_path . DIRECTORY_SEPARATOR . $this->pages[$name]['file']; 
            $func = $this->pages[$name]['function'];
    
            require_once $path;
            call_user_func($func);
        }
    
    }
    
    
    $page = isset($_GET['page']) ? $_GET['page'] : 'profile'; // If no page given default to profile.
    
    $loader = new PageLoader($pages, '/tmp');
    $loader->run($page);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答