PHP前端控制器流混乱 - 切换语句仅测试一次

时间:2012-10-26 05:27:40

标签: php switch-statement mvp front-controller

我是php新手,对此流程感到困惑。在我看来,当$ action变量获得一个新值时,需要多次评估switch($ action)。但我没有看到正在评估的开关($ action)只是一次。我以admin_menu.php为例。如果用户从admin_menu.php中选择<p><a href="index.php?action=show_product_manager">Product Manager</a></p>该怎么办?我没有看到之后调用index.php的位置,所以我看不到自从switch($ action)已经运行以来如何测试新值。

localhost ch21_ex2 # cat index.php 
<?php
// Start session management and include necessary functions
session_start();
require_once('model/database.php');
require_once('model/admin_db.php');

// Get the action to perform
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = 'show_admin_menu';
}

// If the user isn't logged in, force the user to login
if (!isset($_SESSION['is_valid_admin'])) {
    $action = 'login';
}

// Perform the specified action
switch($action) {
    case 'login':
        $email = $_POST['email'];
        $password = $_POST['password'];
        if (is_valid_admin_login($email, $password)) {
            $_SESSION['is_valid_admin'] = true;
            include('view/admin_menu.php');
        } else {
            $login_message = 'You must login to view this page.';
            include('view/login.php');
        }
        break;
    case 'show_admin_menu':
        include('view/admin_menu.php');
        break;
    case 'show_product_manager':
        include('view/product_manager.php');
        break;
    case 'show_order_manager':
        include('view/order_manager.php');
        break;
    case 'logout':
        $_SESSION = array();   // Clear all session data from memory
        session_destroy();     // Clean up the session ID
        $login_message = 'You have been logged out.';
        include('view/login.php');
        break;
}
?>localhost ch21_ex2 # firefox 'view/admin_menu.php'
localhost ch21_ex2 # cat 'view/admin_menu.php'
<?php
    require_once('util/secure_conn.php');  // require a secure connection
    require_once('util/valid_admin.php');  // require a valid admin user
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Guitar Shop</title>
        <link rel="stylesheet" type="text/css" href="main.css"/>
    </head>
    <body>
        <div id="page">
            <div id="header">
                <h1>My Guitar Shop</h1>
            </div>
            <div id="main">
                <h1>Admin Menu</h1>
                <p><a href="index.php?action=show_product_manager">Product Manager</a></p>
                <p><a href="index.php?action=show_order_manager">Order Manager</a></p>
                <p><a href="index.php?action=logout">Logout</a></p>

            </div><!-- end main -->
        </div><!-- end page -->
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我刚刚看到了答案....再次调用index.php:<p><a href="index.php?action=show_order_manager">Order Manager</a></p>

中的index.php?action = show_order_manager