Hello everyone,
I am using php slim for the first time to build APIs but I encountered an error after hosting it on a shared server. Whenever I make an API call through postman I get the error message below, though my Database class is configured correctly and uploaded to the server as well (location: public_html/api/src/Model/). Please help me
{
“statusCode”: 500,
“error”: {
“type”: “SERVER_ERROR”,
“description”: “Class ‘App\Db\Database’ not found”
}
}
Here is my controller:
namespace App\Controllers;
use App\Db\Database;
use Firebase\JWT\JWT;
use Respect\Validation\Exceptions\NestedValidationException;
use Slim\Psr7\Response;
use Respect\Validation\Validator as v;
class LoginController
{
public function __construct($data, Response $response)
{
$validator = v::keySet(
v::key('username', v::stringType()->notEmpty()->setName('username')->length(2, 100)),
v::key('password', v::stringType()->notEmpty()->setName('password'))
);
try {
$validator->assert($data);
} catch (NestedValidationException $e) {
$response->getBody()->write(json_encode([
'responseCode' => "13",
'responseMessage' => $e->getMessages(),
]));
return $response->withHeader('Content-Type', 'application/json');
}
$username = $data['username'];
$password = $data['password'];
$hash_pwd = password_hash($password, PASSWORD_DEFAULT);
$db = Database::getInstance();
$login_query =
$db->select(
'users',
[
'email_address',
'passwords'
],
[
'email_address' => $username,
]
);
if (!$login_query) {
$response->getBody()->write(json_encode([
'responseCode' => '99',
'responseMessage' => "Invalid Login Details"
]));
} else {
$user_hash_pwd = $login_query[0]['passwords'];
$password_verify = password_verify($password, $user_hash_pwd);
if ($password_verify) {
$key = "!@#$%^&*()_+";
$payload = array(
"iss" => "http://localhost",
"aud" => "http://localhost",
'iat' => time(),
'uid' => 1,
'exp' => time() + 10,
);
$token = JWT::encode($payload, $key);
$decoded = JWT::decode($token, $key, array('HS256'));
$response->getBody()->write(json_encode([
'responseCode' => '0',
'responseMessage' => "Logged In",
'responseBody' => [
'token' => $token
]
]));
} else {
$response->getBody()->write(json_encode([
'responseCode' => '99',
'responseMessage' => "Incorrect password"
]));
}
}
}
}