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

时间:2016-01-24 09:41:46

标签: php arrays

所以我正在设置我的第一个“花式”页面系统,我遇到了一个问题。我使用下面看到的代码,它加载“配置文件”页面没有任何问题,默认页面也正常工作。然而,其他两个页面根本没有显示,我似乎无法在URL eighter中请求它们。所有文件都在那里。任何帮助将非常感谢! :)

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 个答案:

答案 0 :(得分:0)

在使用$ _SESSION之前,您需要启动会话:

session_start(); # this should be the very 1st line in your php
if(isset($_SESSION['user_id'])){
    // ...
}
else{
    require($pages[$default_page]['file']);
    echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
    $pages[$default_page]['function']();
    }
}

并确保$pages[$default_page]['function']();设置$_SESSION['user_id']

答案 1 :(得分:0)

您的代码中有一些冗余。你可以缩写它。

$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']();

我建议你将页面加载代码放在一个函数中。我在这里创建了一个简单的类:

<?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);