2016-03-19 86 views
0

我在Windows 64x上使用Apache 2.4.9的Wamp Server 2.5。我在c:\ wamp \ www \ tridrops文件夾下創建了一個使用Slim REST Api項目的PHP。我有我的index.php,我試圖在c:\ wamp \ www \ tridrops \ tridrops \ ver \ index.php中執行。Wamp服務器 - 無法執行任何PHP Rest API

我的PHP代碼:

<?php 

use Slim\Slim; 
require_once '../include/DbHandler.php'; 
require_once '../libs/Slim-2.x/Slim/Slim.php'; 

\Slim\Slim::registerAutoloader(); 

$app = new Slim(); 

$app->run(); 

/*** 
* Creating NEW CATEGORY in db 
* method POST 
* params - category_name, category_description 
* url - /categories/ 
*/ 
$app->post('/categories', function() use ($app) { 

// Check for required params 
verifyRequiredParams(array('name', 'desc')); 

$response = array(); 
// Reading post parameters 
$category_name = $app->request()->post('name'); 
$category_description = $app->request()->post('desc'); 

$db = new DbHandler(); 

// Creating new Category 
$categoryId = $db->createCategory($category_name, $category_description); 

if ($categoryId != NULL) { 
    $response["error"] = false; 
    $response["message"] = "Category Created Successfully with Category "; 
    $response["categoryId"] = $categoryId; 
} else { 
    $response["error"] = true; 
    $response["message"] = "Failed to create Category. Please try again."; 
} 
echoRespnse(201, $response); 

}); 

/** 
* Getting List of Categories 
* method GET 
* url /categories 
*/ 
$app->get('/categories', function() { 
$response = array(); 
$db = new DbHandler(); 

// fetching all categories 
$result = $db->getAllCategories(); 

$response["error"] = false; 
$response["categories"] = array(); 

// looping through results 
while ($categories = $result->fetch_assoc()) { 
    $tmp = array(); 
    $tmp["categoryId"] = $categories["categoryId"]; 
    $tmp["category_name"] = $categories["category_name"]; 
    $tmp["categoy_isparent"] = $categories["categoy_isparent"]; 
    $tmp["category_description"] = $categories["category_description"]; 

    array_push($response["categories"], $tmp); 
} 

echoRespnse(200, $response); 
}); 

?> 

DbHandler.PHP文件

<?php 

// DbHandler.php 

/** 
* Class to handle DB operations 
* CRUD methods for database 
*/ 

class DbHandler { 
    private $conn; 

function __construct() { 
    require_once dirname(__FILE__) . './DbConnect.php'; 
    // Opening db connection 
    $db = new DbConnect(); 
    $this->conn = $db->connect();    
} 

/*------------------ category table methods ------------ */ 
/** 
* Creating new Category 
* @param String $category_name 
* @param String $category_description 
* @param boolean $category_isparent 
*/ 
public function createCategory($category_name, $category_description) { 
    $response = array(); 

    // First ccheck if category already exists 
    if (! $this->isCategoryExists($category_name)) { 
     // insert stmt 
     $stmt = $this->conn->prepare("INSERT INTO category(category_name, category_description) values(?, ?)"); 
     $stmt->bind_param("ssss", $category_name, $category_description); 

     $result = $stmt->execute(); 

     $stmt->close(); 

     // Check for successful insertion 
     if ($result) { 
      // Category Created Successfully 
      return SUCCESSFUL; 
     } else { 
      // fAILED TO INSERT 
      return FAILED; 
     }   
    } else { 
     // Same Category Exists 
     return EXISTS; 
    } 

    return $response; 
} 

/** 
* Fetch all Categories 
*/ 
public function getAllCategories() { 
    $stmt = $this->conn->prepare("SELECT * FROM category"); 
    $stmt->execute(); 
    $cats = $stmt->get_result(); 
    $stmt.close(); 

    return $cats; 
} 

的.htaccess

# tridrops/ver/.htaccess 

RewriteEngine On 


# Always set these headers. 
#Header always set Access-Control-Allow-Origin "*" 
#Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type" 
#Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS" 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [QSA,L] 

我試着在谷歌高級REST客戶端執行的categoriesPOST & GET,但我繼續獲得404

迴應:

<title>404 Not Found</title> 
</head><body> 
<h1>Not Found</h1> 
<p>The requested URL /tridrops/tridrops/ver/categories/ was not found on this server.</p> 
<hr> 
<address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 81</address> 
</body> 

任何想法,爲什麼我只得到這個錯誤。
Wamp在端口81上運行。我可以很好地執行phpadmin。爲什麼這是造成問題的原因,從幾天起就無法弄清楚。

你能幫我解決這個錯誤嗎?

任何幫助都非常感激。

非常感謝。

回答

1

看起來問題可能是重寫問題。我看到你的重寫規則在你的htaccess文件中被註釋掉了。你想將每一條路由到index.php?

你也許可以通過訪問... index.php/categories來測試這個問題,並且該頁面應該加載。

編輯:試試這個htaccess文件。

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [QSA,L] 
+0

感謝您的快速響應。我編輯了我的重寫規則 - 重寫規則^(。*)$%{ENV:BASE} index.php [QSA,L] 重新啓動的wamp&嘗試了http:// localhost:81/tridrops/tridrops/ver/categories in ARC和瀏覽器。兩者都給出相同的響應,即404 – Tvd

+0

我沒有在httpd.conf或任何文件中的項目文件夾中添加任何內容。有什麼要求嗎?我閱讀了幾個鏈接以添加\ www中每個文件夾的虛擬主機。這是否涉及我的問題? – Tvd

+1

我認爲會有兩件事要檢查。確保mod_rewrite模塊已打開。此外,重寫規則正在尋找可能會破壞根文件夾的環境基礎。 試試看起來像這樣的htaccess。 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule^index.php [QSA,L]

1

正如@Steve Parish所說,它看起來像你沒有你的URL重寫設置/啓用。當Steve幫助你完成重寫工作時,我還建議檢查是否啓用了重寫工作所需的模塊。試試這裏的答案,檢查是否啓用了mod_rewrite: How to check if mod_rewrite is enabled in php?

如果不是,您的.htaccess基本上會被忽略。最好在與應該工作的htaccess語法戰鬥之前檢查一下。希望能幫助到你!

+0

謝謝@Tiago,我狡猾,mod_rewrite可用。 Apache/2.4.9(Win64)PHP/5.5.12 mod_rewrite模塊可用 – Tvd