This is my Index.php file:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
//User Routes
require '../src/routes/users.php'; // This not working
require '../src/routes/org.php'; // Only This working
$app->run();
.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
When there is only one require file it's working fine but when adding two or multiple files then only last one working and others giving page not found error. I'm new in slim and PHP so I have no idea what's wrong.
users.php
<?php
/**
* User: y34h1a
* Date: 2/10/17
* Time: 3:47 AM
*/
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
//Get All Customers
$app->get('/api/users', function (Request $request, Response $response){
$sql = "Select * from users";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$users['users'] = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($users);
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//GET SINGLE USER
$app->get('/api/user/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$sql = "Select * from users WHERE id = $id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$user['user'] = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($user);
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//ADD USER
$app->post('/api/user/add', function (Request $request, Response $response){
$name = $request->getParam('name');
$email = $request->getParam('email');
$password = $request->getParam('password');
$sql = "Insert into users(name,email, password) values (:name, :email, :password)";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
$stmt->execute();
echo '{"notice": {"text": "Customer Added"}}';
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//UPDATE USER
$app->put('/api/user/update/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$name = $request->getParam('name');
$email = $request->getParam('email');
$password = $request->getParam('password');
$sql = "UPDATE users SET
name = :name,
email = :email,
password = :password
WHERE id = $id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
$stmt->execute();
echo '{"notice": {"text": "Customr UPDATED"}}';
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//DELETE USER
$app->get('/api/user/delete/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$sql = "DELETE FROM users WHERE id = $id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->execute();
$db = null;
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
org.php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
//Get All Orgs
$app->get('/api/orgs', function (Request $request, Response $response){
$sql = "Select * from org";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$users['orgs'] = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($users);
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//GET SINGLE ORG
$app->get('/api/org/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$sql = "Select * from org WHERE id = $id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$user['org'] = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($user);
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//ADD ORG
$app->post('/api/org/add', function (Request $request, Response $response){
$user_id = $request->getParam('user_id');
$name = $request->getParam('name');
$address = $request->getParam('address');
$sql = "Insert into org(user_id, name, address) values (:user_id, :name, :address)";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':address', $address);
$stmt->execute();
echo '{"notice": {"text": "Organization Added"}}';
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//UPDATE ORG
$app->put('/api/org/update/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$name = $request->getParam('name');
$address = $request->getParam('address');
$sql = "UPDATE org SET
name = :name,
address = :address
WHERE id = $id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':address', $address);
$stmt->execute();
echo '{"notice": {"text": "Organization Deleted"}}';
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});
//DELETE ORG
$app->delete('/api/org/delete/{id}', function (Request $request, Response $response){
$id = $request->getAttribute('id');
$user_id = $request->getParam('user_id');
$org_id = $request->getParam('org_id');
$sql = "DELETE FROM org WHERE id = $id AND user_id = $user_id";
try{
//Get DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->execute();
$db = null;
echo '{"notice": {"text": "Sheet Deleted"}}';
}catch(PDOException $e){
echo '{"error":{"text":'.$e->getMessage().'}';
}
});