PHP Slim:一个文件中的多个函数会导致错误

时间:2015-05-08 08:52:40

标签: php slim

我对PHP slim相当新,但它实际上对我很有用。但我有一个问题,我觉得很奇怪!我有一个包含所有API函数的PHP文件,用于获取或编辑数据库中的数据。这些功能中的每一个都能很好地分离,但是当我将它们全部取消注释并且它们都处于活动状态时,当我想要使用它们或者在浏览器中直接浏览它们时,我会收到404错误。

在这里你可以看到我想要使用的所有功能:

//PHPSLIM
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();


$categories = new \Slim\Slim();
$categories = \Slim\Slim::getInstance();

$categories->get('/categories', function(){
    global $conn, $servername, $username, $password, $dbname;
    mysql_connect($servername, $username, $password);
    mysql_select_db($dbname);
    $result = mysql_query("SELECT `categoryID`, `categoryName` FROM `category`");

    while($line = mysql_fetch_array($result, MYSQL_NUM)) {
        $cat[] = $line;
    } 

    echoResponseCat(200, $cat);

});

function echoResponseCat($status_code, $response) {
    global $categories;
    $categories->status($status_code);
    header('Content-Type: application/json');
    $categories->contentType('application/json');
    echo json_encode($response,JSON_NUMERIC_CHECK);
}

$categories->run();



//ALTER ORDER STATUS

$status = new \Slim\Slim();
$status = \Slim\Slim::getInstance();

$status->get('/orderPrepared/:orderID', function ($orderID) {
    global $conn, $servername, $username, $password, $dbname;
    if(isset($orderID)){
        $sql = "UPDATE `order` SET `orderStatus`= 'done' WHERE `orderID` = '$orderID'";  

        mysql_connect($servername, $username, $password);
        mysql_select_db($dbname);  
        $result = $conn->query($sql);
    }

});
$status->run();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

//GET ALL ORDERS
$app->get('/orders', function(){
$extranames = "";
$extrasprice = 0;
    global $conn, $servername, $username, $password, $dbname;
    $sql = "SELECT `orderID`, `customerID`, `storeID`, `orderDate`, `orderDeliveryDate`, `orderStatus` FROM `order`";

    mysql_connect($servername, $username, $password);
    mysql_select_db($dbname);
    $query = mysql_query("SELECT `orderID`, `customerID`, `orderDate`, `orderDeliveryDate`, `orderStatus` FROM `order`");
    $ordersall = array();

    while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
        $orders[] = $line;
    }

    $count = 0;
    $extrasprice = 0;

    $ordersall = array();
    $orderKeys = array('orderID', 'customerID', 'customerName', 'orderDate', 'orderDeliveryDate', 'orderStatus');
    $orderKeys = array_combine($orderKeys, $orderKeys);   

    foreach($orders as $key => $looporders){
        $extracounter = 1;

        $sql = "SELECT `orderdetailID`, `productID`, `orderdetailAantal`  FROM `orderdetail` WHERE `orderID` = '$looporders[orderID]'";
        $result = $conn->query($sql);
        //echo("orderID " . $looporders["orderID"] . " " . $result->num_rows ."/");
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
            $sql2 = "SELECT `productTitle`, `productPrijs`  FROM `products` WHERE `productID` = '$row[productID]'";
            $result2 = $conn->query($sql2);
                if($result2->num_rows > 0) {
                    while($row2 = $result2->fetch_assoc()){
                        $productTitle = $row2["productTitle"];
                        $productPrijs = $row2["productPrijs"];

                    }
                } 
            $sql3 = "SELECT `customerName` FROM `customers` WHERE `customerID` = '$looporders[customerID]'";
            $result3 = $conn->query($sql3);
                if($result3->num_rows > 0) {
                    while($row3 = $result3->fetch_assoc()){
                        $customerName = $row3["customerName"];
                    }
                }    
            $sql4 = "SELECT `extraID` FROM `orderdetailextra` WHERE `orderdetailID` = '$row[orderdetailID]'";
            $result4 = $conn->query($sql4);

            if ($result4->num_rows > 0) {
                while($row4 = $result4->fetch_assoc()) {
                    $sql5 = "SELECT `extraName`, `extraPrice` FROM `extra` WHERE `extraID` = '$row4[extraID]'";
                    $result5 = $conn->query($sql5);
                    if ($result5->num_rows > 0) {       
                     while($row5 = $result5->fetch_assoc()) {   

                        $extranames = $extranames . $row5["extraName"]. ', ';
                        $extrasprice += $row5["extraPrice"];    
                     }
                    }  
                 }
                } 

                $orderID = $looporders["orderID"];
                if(!isset($ordersall[$orderID])){
                    $ordersall[$orderID] = array_intersect_key($looporders,$orderKeys);
                    $ordersall[$orderID]["customerName"] = $customerName;
                    $ordersall[$orderID]['details'] = array();
                    $newdata = array(

                        "orderdetailID" => $row["orderdetailID"],
                        "productTitle" => $productTitle,
                        "productPrijs" => $productPrijs,
                        "aantal" => $row["orderdetailAantal"],
                        "extras" => substr($extranames, 0, -2),                
                        "extrasPrice" => $extrasprice 

                    );
                    array_push($ordersall[$orderID]['details'], $newdata);


                }
                else if(isset($ordersall[$orderID])){
                    $newdata = array(
                        "orderdetailID" => $row["orderdetailID"],
                        "productTitle" => $productTitle,
                        "productPrijs" => $productPrijs,
                        "aantal" => $row["orderdetailAantal"],
                        "extras" => substr($extranames, 0, -2),                
                        "extrasPrice" => $extrasprice 
                    );
                    array_push($ordersall[$orderID]['details'], $newdata);


                }                
                $count++;
                $extranames ="";
                $extrasprice = 0;
            }
        }


    }
    echoResponse(200, $ordersall);

});

$app->run();



//GET ALL ORDERS FOR 1 CUSTOMER          
$app->get('/orderoverview/:customerID', function ($customerID) {

global $conn, $servername, $username, $password, $dbname;

//1.1   GET ORDER (ID)  AND NAME OF CUSTOMER
$sql = "SELECT `customerName` FROM `customers` WHERE `customerID` = $customerID";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $customername = $row["customerName"];
    }
} 
else {
    echo "Customer name failure";
}

$sql = "SELECT `orderID` FROM `order` WHERE `customerID` = $customerID";
$result = $conn->query($sql);
if($result->num_rows > 1){
    echo "You already have a pending order! <br>";
}
else if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $orderID = $row["orderID"];
    }
    step2($orderID, $customername);
} else {
    echo "GET orderID failure";
}
});
$app->run();

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

您在每个控制器后调用run。但你只需要这样做一次。适合您的整个应用

$app = new \Slim\Slim();

$app->get('/orderPrepared/:orderID', function ($orderID) {

});

..分配所有控制器..

$app->run();