controller design of a REST API in PHP
Unless I am missing something in your requirements, I don't think there is a terrific need for the overhead of a class. (I could be wrong)

I do spy some opportunities to ratchet up your code though.  Consider these points and if you decide to further develop your script into a class, then this should move you in that direction with some single responsibility thinking.


Don't extract data until you are sure that you will use it.
Don't Repeat Yourself (DRY). Don't make the server repeat any operations that provide results that its already provided.
switch() blocks are verbose, but they are appropriately used when you are performing multiple evaluations on the same variable.




require_once("../classes/MobileRestHandler.php");

function getId() {
    return ['id' =&gt; $_GET["id"] ?? ''];
}

function getPost() {
    $defaults = array_fill_keys(['name', 'model', 'color'], '');
    $posted = json_decode(file_get_contents('php://input'), true);
    return array_replace($defaults, array_intersect_key($posted, $defaults));
}

function getIdAndPost() {
    return array_merge(getId(), getPost());
}

switch ($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        $result = empty($_GET['id'])
            ? $mobileRestHandler-&gt;getAllMobiles();
            : $mobileRestHandler-&gt;getMobile(getId());
        break;
    case 'POST':
        $result = $mobileRestHandler-&gt;addMobile(getPost());
        break;
    case 'PUT':
        $result = $mobileRestHandler-&gt;editMobile(getIdAndPost());
        break;
    case 'DELETE':
        $result = $mobileRestHandler-&gt;deleteMobile(getId());
        break;
    default:
        $result = '404 - ' . $mobileRestHandler-&gt;getHttpStatusMessage(404);
}

exit($result);


Admittedly, my untested script doesn't validate, sanitize, or throw any errors, but your script didn't seem to mind these relevant topics.

p.s. For the record, I don't endorse the technique of deleting a record based on $_GET data -- if a crawler would hapen to come upon your script and some valid ids, it could vanquish records during the simple act of crawling.  When manipulating data stores, always use $_POST.