php – Better way to implement single end point REST API?

I want to ask question about a better way for implementing single endpoint REST.

I want to build a rest API but I want it to only have single endpoint like: graphql, where you only have like: host/graphql.

I already doing this example in laravel where you need to put a key and method to go to a certain API.

The body look like this (example of calling product API with single endpoint).

Calling get all product:

    {
     "key":"product/all",
     "method":"GET"
    }

And this is the get one (product/{id}):

    {
    "key":"product",
    "method":"GET",
    "payload":"{"id":"1"}"
    }

This is also work fine with post, put and delete.

Here is the single endpoint code (I use php laravel as example because it fast to make API):

    public function bridge(Request $request)
    {

        $host = "http://127.0.0.1:8000/api/";
        if ($request->has("payload")) {
            $bounds = html_entity_decode($request->payload);
            $payload = json_decode($bounds, true);
        }
        if (strtoupper($request->method) == "POST") {
            try {
                $url = $host . $request->key;
                $request = Request::create($url, 'POST', ());
                $response = Route::dispatch($request);
                return $response;
            } catch (Throwable $th) {
                $data("data") = ();
                $data("success") = false;
                $data("code") = 500;
                $data("message") = $th->getMessage();
                return $data;
            }
        } else if (strtoupper($request->method) == "PUT") {
            try {
                $url = $host . $request->key . "https://codereview.stackexchange.com/" . $payload('id');
                $request = Request::create($url, 'PUT', ());
                $response = Route::dispatch($request);
                return $response;
            } catch (Throwable $th) {
                $data("data") = ();
                $data("success") = false;
                $data("code") = 500;
                $data("message") = $th->getMessage();
                return $data;
            }
        } else if (strtoupper($request->method) == "DELETE") {
            try {
                $url = $host . $request->key . "https://codereview.stackexchange.com/" . $payload('id');
                $request = Request::create($url, 'DELETE', ());
                $response = Route::dispatch($request);
                return $response;
            } catch (Throwable $th) {
                $data("data") = ();
                $data("success") = false;
                $data("code") = 500;
                $data("message") = $th->getMessage();
                return $data;
            }
        } else {
            $url = $host . $request->key;
            try {
                if ($request->has("payload")) {
                    $url = $host . $request->key . "/" . $payload('id');
                }
                $request = Request::create($url, 'GET');
                $response = Route::dispatch($request);
                return $response;
            } catch (Throwable $th) {
                $data("data") = ();
                $data("success") = false;
                $data("code") = 500;
                $data("message") = $th->getMessage();
                return $data;
            }
        }
    }

As you can see there. It is calling the API twice “bridge” and the “key” API.

The cons here:

  • call request twice;
  • you can only put one /{id} variable (or more if you declare it).

Pros:

  • it only one endpoint so it will be easy to make an api helper in the frontend (maybe XD).

Can you show me a better way of doing this?
Tell me your tips and thought or maybe show some code (any language is fine).