Answer the question
In order to leave comments, you need to log in
How to do validation per request in index.php PHP?
Good afternoon, I'm making an API in PHP, and the question arose of how to check for a method, that is, now only api/users works, but how to do api/cars, for example?
https://klisl.com/php-api-rest.html - sources
<?php
require_once 'UsersApi.php';
try {
$api = new usersApi();
echo $api->run();
} catch (Exception $e) {
echo json_encode(Array('error' => $e->getMessage()));
}
Answer the question
In order to leave comments, you need to log in
You inherit from and enter into abstract Api
the property
Next, you will need to resolve the routing. all your requests go to index.php
There must be a router that, depending on the parameters passed, will call the necessary code
for simplicity, it will
look at the superglobal array and see where it comes.
By this property, you do or call the classes you need with api public $apiName;
cars
if
switch
echo $_SERVER['REQUEST_METHOD']; //'GET', 'HEAD', 'POST', 'PUT'.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question