Php switch Case当输入错误的url时,为什么我的默认页面不会被触发

时间:2013-04-04 13:29:43

标签: php html

您好我正在处理页面切换代码,默认页面似乎没有加载由于某种原因可以有人帮忙吗?目前我可以输入一个随机网址,但它只显示找不到的网页。

默认显示触发PHPMyAdmin页面错误
这是views/404.php文件夹 但它没有。我似乎明白为什么它没有。

<?php
//begin the webpage
//index page selector placed into an array for options for the user to select


//session started recording attributes
session_start();

error_reporting(0);

//including other view class and model class function to be used on this page
include 'views/viewClass.php';
include 'classes/modelClass.php';


class PageSelector {
    public function run() {
        //has the user decided which page to go to?
        //if not then we will direct him to the homepage
        if(!$_GET['page']) {
            $_GET['page'] = 'Home';
        }

        //extending a new model class
        $model = new Model;

        //get the page information
        $pageInfo = $model -> getPageInfo($_GET['page']);

        //select which page to include when the user clicks
        //on either register/home/my account/my bids/my sell/completed auctions/edit etc..etc..etc..
        switch($_GET['page']) {

            case 'Register':
                include 'views/register.php';
                $view = new HomeView($pageInfo, $model);
                break;

            case 'Home':
                include 'views/homeView.php';
                $view = new HomeView($pageInfo, $model);
                break;

            case 'My Account':
                include 'views/myaccount.php';
                $view = new myaccount($pageInfo, $model);
                break;

            case 'My Bids':
                include 'views/mybids.php';
                $view = new Mybids($pageInfo, $model);
                break;

            case 'My Sell':
                include 'views/mysell.php';
                $view = new mysell($pageInfo, $model);
                break;

            case 'Completed Auctions':
                include 'views/checkout.php';
                $view = new checkout($pageInfo, $model);
                break;

            case 'editProduct':
                include 'views/editProductView.php';
                $view = new EditProductView($pageInfo, $model);
                break;

            case 'addProduct':
                include 'views/addProductView.php';
                $view = new AddProductView($pageInfo, $model);
                break;

            case 'deleteProduct':
                include 'views/deleteProductView.php';
                $view = new DeleteProductView($pageInfo, $model);
            break;

            case 'deleteUser':
                include 'views/deleteUserView.php';
                $view = new DeleteUserView($pageInfo, $model);
            break;

            case 'search':
                include 'views/search.php';
                $view = new search($pageInfo, $model);
            break;   

            case 'product':
                include 'views/productView.php';
                $view = new ProductView($pageInfo, $model);
            break;

            default: 
                include 'views/404.php';
                $view = new error($pageInfo,$model);




        }
        echo $view -> displayPage();

    }    

}

$pageSelect = new PageSelector();
$pageSelect -> run();
//calls the selector to run after user has selected.

2 个答案:

答案 0 :(得分:0)

尝试更改

if(!$_GET['page']) {

if(!isset($_GET['page']) || $_GET['page'] == '') {

答案 1 :(得分:0)

我将假设您正确地将404路由到此文件。发生的事情是因为404没有$ _GET ['page']你的代码将自己设置为home并加载主页。

假设这是index.php文件,我会添加一个条件来检查这个并将默认值更改为案例404.这是完整的新代码。

<?php
//begin the webpage
//index page selector placed into an array for options for the user to select


//session started recording attributes
session_start();

error_reporting(0);

//including other view class and model class function to be used on this page
include 'views/viewClass.php';
include 'classes/modelClass.php';


class PageSelector {
    public function run() {
    //Check to make sure they are loading a legit page first
    if($_SERVER['PHP_SELF'] != 'index.php'){
        $_GET['page'] = '404';
    }
    //has the user decided which page to go to?
    //if not then we will direct him to the homepage
    if(!$_GET['page']) {
        $_GET['page'] = 'Home';
    }

    //extending a new model class
    $model = new Model;

    //get the page information
    $pageInfo = $model -> getPageInfo($_GET['page']);

    //select which page to include when the user clicks
    //on either register/home/my account/my bids/my sell/completed auctions/edit etc..etc..etc..
    switch($_GET['page']) {

        case 'Register':
            include 'views/register.php';
            $view = new HomeView($pageInfo, $model);
            break;

        case 'Home':
            include 'views/homeView.php';
            $view = new HomeView($pageInfo, $model);
            break;

        case 'My Account':
            include 'views/myaccount.php';
            $view = new myaccount($pageInfo, $model);
            break;

        case 'My Bids':
            include 'views/mybids.php';
            $view = new Mybids($pageInfo, $model);
            break;

        case 'My Sell':
            include 'views/mysell.php';
            $view = new mysell($pageInfo, $model);
            break;

        case 'Completed Auctions':
            include 'views/checkout.php';
            $view = new checkout($pageInfo, $model);
            break;

        case 'editProduct':
            include 'views/editProductView.php';
            $view = new EditProductView($pageInfo, $model);
            break;

        case 'addProduct':
            include 'views/addProductView.php';
            $view = new AddProductView($pageInfo, $model);
            break;

        case 'deleteProduct':
            include 'views/deleteProductView.php';
            $view = new DeleteProductView($pageInfo, $model);
        break;

        case 'deleteUser':
            include 'views/deleteUserView.php';
            $view = new DeleteUserView($pageInfo, $model);
        break;

        case 'search':
            include 'views/search.php';
            $view = new search($pageInfo, $model);
        break;   

        case 'product':
            include 'views/productView.php';
            $view = new ProductView($pageInfo, $model);
        break;

        case '404': 
            include 'views/404.php';
            $view = new error($pageInfo,$model);
        break;



        }
        echo $view -> displayPage();

    }    

}

$pageSelect = new PageSelector();
$pageSelect -> run();
//calls the selector to run after user has selected.