2

I have created this code:

header("Content-Type: application/json"); $method = $_SERVER['REQUEST_METHOD']; $request = explode("/", substr(@$_SERVER['PATH_INFO'], 1)); switch ($method) { case 'PUT': break; case 'POST': do_something_with_post($request); break; case 'GET': do_something_with_get($request); break; case 'DELETE': do_something_with_delete($request); break; default: handle_error($request); break; } 

Now suppose that I execute this command:

curl -X GET http://localhost/api/method/1 

so in the switch is fired the GET case, how I can call the function method and pass the parameter 1? how I should configure the code on the switch? Someone could help me to figure out?

2 Answers 2

2

Here's a simplified way to route request paths to classes. This example assumes your base path is /app and you have a sub-folder called /classes. The class name and file name must match for this to work.

Example request:

curl -X POST http://localhost/api/user/123

Example route:

File....: /app/classes/api/User.php Class...: new User() Action..: postAction( $arg1 = 123 ) 

...

// default route $base = rtrim( str_replace( '\\', '/', __DIR__.'/app' ), '/' ); $area = 'api'; // area (/api, /test, etc.) $class = 'home'; // class name (Home.php -> new Home(), etc.) // parse request $verb = strtolower( @$_SERVER['REQUEST_METHOD'] ); $path = parse_url( @$_SERVER['REQUEST_URI'], PHP_URL_PATH ); $args = explode( '/', trim( $path, '/' ) ); // extract area/class from request path if( count( $args ) ) { $area = array_shift( $args ); } if( count( $args ) ) { $class = array_shift( $args ); } // finalize class name and file path $class = str_replace( ' ', '', ucwords( str_replace( '-', ' ', $class ) ) ); $file = $base .'/classes/'. $area .'/'. $class.'.php'; $output = null; // load/execute class if( is_file( $file ) ) { include_once( $file ); if( class_exists( $class ) ) { $callable = [ new $class(), $verb.'Action' ]; if( is_callable( $callable ) ) { $output = call_user_func_array( $callable, $args ); } } } // send response output... if( is_null( $output ) === false ) { // ... } else { // handle error } exit; 
Sign up to request clarification or add additional context in comments.

Comments

1

You'll want to parse the URI:

$pieces = explode('?', $_SERVER['REQUEST_URI']); $endpoint = $pieces[0]; $endpoint_parts = explode('/', $endpoint); $api_method = $endpoint_parts[2]; $param = $endpoint_parts[3]; 

Then you can invoke method from your URL with 1 like this:

$api_method($param); 

3 Comments

Okay, now is a bit clear. But I have few question: 1. How can I call the exact method from $api_method($param); that is only a variable? - 2. Where I should find all the methods? I mean the organization for methods GET - POST - PUT - DELETE. I'm going to create classes for each category. In my example I mentioned method as a function, but imagine that I've an application that manages users, so I would have an API file that manages the user, other components that handle user. How can I access these files and invoke the appropriate methods simply recognizing them in the switch?
1 - read up on variable functions
For the point two, I mean, essentially: where should I place all methods of the API for each content, like the class User, class Foo, class Example, etc...?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.