phprestapi-design

Create web API with /path/to/{unique-path}/ design


I am currently learning web development and working on my own small website. I followed an tutorial by W3Schools and used the learned to create a working login page. It takes the input and sends it using jQuery.

Now I wanted to learn more about how to create an API to manage users and found a guide on the best practices when designing a REST Api by Microsoft. It also contained an example of what a user and order management API system could look like. In the article they show a similar table looking like this:

Resource POST GET PUT DELETE
/users Create user Get all users Bulk update users Delete all users
/users/{id} Error Get user Info Update user details Delete user
/users/{id}/orders Create order Get all orders Bulk update orders Remove all orders
/users/{id}/orders/{id} Error Get Order Info Update order details Delete order

How can I implement this into my project and is it possible with just PHP and JavaScript?
When searching for it online I only found C# answers

My request for logging in currently looks like this:

function login() {
    var username = $("#username-input").val();
    var password = $("#password-input").val();
    $.post("/ajax/account/login.php", 
        {
            username: username,
            password: password
        },
        function (data, status, jqXHR) {
            console.log(jqXHR.responseText)
            data = JSON.parse(get_ajax_response_text(jqXHR))
            if(data.hasOwnProperty("error")) {
                showError("<b>ERROR:</b> " + data["message"]);
            } else {
                setTimeout(function() {
                    window.location.href = "/hub";
                }, 1000)
                showSuccess(data["message"]);
            }
        },
    );
}

Solution

  • I'm now using Symfony, which allows me to do exactly what I wanted. They have a great Tutorial on SymfonyCasts.com which goes through everything you need to know.

    I'm leaving the question open for others to find