0
\$\begingroup\$

This code is meant to be a very simple RESTful API.

The simple router takes the URI request and maps it to a function with a matching name. E.g. www.domain.com/bill maps to function bill(). Then bill will provide functions that will be REST endpoints - DELETE bill/id, GET bill (which gets all bills for a user), PUT bill/id, POST bill, etc.

I would like to make the code more object oriented. At the moment I only have one class, Token.

Any suggestions on where to add more classes in this small application would be helpful.

index.php

include token.php; // not included here
include bill.php;

// Parse URI
$actions = explode("/", $_SERVER['REQUEST_URI']);

// JWT Secret
$secret = "rvMuQ1MJ002IeWcl09TT4grwUxz41sSR";

// 'Router' 
if(function_exists($actions[1])) {
    call_user_func_array($actions[1], array($secret));
}

bill.php

function bill($secret) {

    // Get all bills for user 
    if($_SERVER['REQUEST_METHOD'] === 'GET') {  

        // Make sure token is valid
        $id = checkToken($secret);
        if($id != 0) {

            $refreshed_token = new Token($id, $secret, null);

            // Retrieve the bills
            $db = new PDO("sqlite:../database/database.sqlite");
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $query = "SELECT bills.id, bills.user_id, billers.name, billers.category, bills.amount, bills.due, bills.status FROM bills INNER JOIN billers ON bills.biller_id = billers.id WHERE bills.user_id = :user_id";

            $stmt = $db->prepare($query);

            //if(!$stmt) { print_r($db->errorInfo()); }

            $stmt->bindParam(':user_id', $id);

            if($stmt->execute()) {

                $bills = $stmt->fetchAll();
                echo json_encode([
                    'code' => 200, 
                    'message' => 'OK',
                    'bills' => $bills,
                    'token' => $refreshed_token->getTokenString()]);

            } else {

                echo json_encode([
                    'code' => 404, 
                    'message' => 'Not found',
                    'bills' => '',
                    'token' => $refreshed_token->getTokenString()]);

            }


        } else {

            echo json_encode(["code" => 401, "message" => "Unauthorized"]);

        }

    }

    ..... other similar methods DELETE, PUT, etc.

}
\$\endgroup\$
  • \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does. \$\endgroup\$ – Phrancis Mar 26 '18 at 4:31
  • \$\begingroup\$ The code works. I have no idea what your issue is. Also, you changed my question. I asked how to make it more object oriented, not 'dispatching'. It's really annoying to me that I don't have any rights here. You just close my question for no good reason and I don't get my code reviewed. That doesn't help me at all. And I don't know why you guys at stackoverflow do this kind of thing. It's like you hate new users. I just don't get it. I posted code here to try to improve myself and you shot me down without even a valid reason. Why does this site even exist if you guys refuse to help newbies? \$\endgroup\$ – user165273 Mar 27 '18 at 0:44
  • \$\begingroup\$ Since there's no way for me to fight what you've done to me here, I'm just going to say this: from now on, I'm just going to ask my questions elsewhere. Quora, forums, ANYWHERE ELSE but here. Because this is ridiculous. This site is so elitist and mean and unhelpful it's not even funny. \$\endgroup\$ – user165273 Mar 27 '18 at 0:45
  • \$\begingroup\$ Hey again, I'm very sorry that this has been your experience, that's definitely not what we want site visitors to feel like. I voted to re-open it, after your clarification. If you prefer though, you may also delete your question. Sorry again... \$\endgroup\$ – Phrancis Mar 27 '18 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy