0

I have Wamp Server 2.5 with Apache 2.4.9 on Windows 64x. I created a PHP using Slim REST Api project under c:\wamp\www\tridrops folder. I have my index.php, that I am trying to execute at c:\wamp\www\tridrops\tridrops\ver\index.php .

My PHP code :

<?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 file

<?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]

I tried to execute POST & GET of categories in Google Advanced REST Client, but I keep on getting 404.

RESPONSE:

<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>

Any idea why am I getting this error only.
Wamp is running on port 81. I can execute phpadmin perfectly well. Why this is causing problem, can't figure out since days.

Can you please help me fix this error.

Any help is highly appreciative.

Thanks a lot.

Tvd
  • 4,463
  • 18
  • 79
  • 125

2 Answers2

1

Looks like the issue could be a rewrite problem. I see your rewrite rule is commented out in your htaccess file. You want to route everythig to the index.php right?

You can probably test that this is the issue by visiting ...index.php/categories and that page should load.

EDIT: try this htaccess file.

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Steve Parish
  • 1,824
  • 2
  • 14
  • 12
  • Thanks for quick response. I edited my Rewrite rule - RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] Restarted wamp & And tried http://localhost:81/tridrops/tridrops/ver/categories in ARC & browser. Both gives same response i.e. 404 – Tvd Mar 19 '16 at 23:21
  • I have not added anything in respect to my project folder in httpd.conf or any files. Is anything required ? I read in few links to add virtual host of each folder in \www . Does this refer to my problem ??? – Tvd Mar 19 '16 at 23:23
  • 1
    I think there would be two things to check. Make sure the mod_rewrite module is switched on. Also the rewrite rule is looking for the environment base which may be wamps root folder. Try a htaccess that looks like this. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L] – Steve Parish Mar 20 '16 at 10:52
  • I tried the way you have mentioned. But no success.My DB_HOST -- define('DB_HOST', 'localhost:81'); As I said my wamp is on port 81, so I have written like this. Is this correct ? – Tvd Mar 20 '16 at 20:36
  • 1
    Port will not matter. Sorry it was bad formatting on my behalf. I will update my answer to include a new htaccess file to try. – Steve Parish Mar 21 '16 at 11:54
  • Steve Parish Ok. As mentioned by @Tiago, I checked with mod_rewrite & it is enabled. I have shared the results on his answer. I also updated my htaccess as you have shown, but unfortunately no success & same results i.e. 404 as above. – Tvd Mar 22 '16 at 16:23
  • I tried using another method thru http://www.9lessons.info/2014/12/create-restful-services-using-slim-php.html . Using that method also, I am facing problem in post. Can you please check my post with name "Trupti". I have just tried with get & post. Get works (no params passed, just lists all records), but post giving problems. I think it is not able to retrieve the params passed thru ARC in post. Do note my echo "Update" - it is empty. I believe it should show the params passed. Please check out & help me in either way. Thanks a lot. – Tvd Mar 22 '16 at 17:32
1

As @Steve Parish has said, it looks like you don't have your URL rewriting setup/enabled. While Steve is helping you with getting your rewrite working properly, I would also recommend checking that the module required for rewrite to work is enabled. Try out the answer here to check that mod_rewrite is enabled: How to check if mod_rewrite is enabled in php?

If it isn't, your .htaccess would essentially be ignored. Best to check this before fighting htaccess syntax that should be working. Hope it helps!

Community
  • 1
  • 1
Tiago
  • 1,984
  • 1
  • 21
  • 43
  • Thanks @Tiago, I chekced and mod_rewrite is available. Apache/2.4.9 (Win64) PHP/5.5.12 mod_rewrite Module Available – Tvd Mar 22 '16 at 16:00