R
R
rshruslan2019-07-08 08:48:54
PHP
rshruslan, 2019-07-08 08:48:54

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

2 answer(s)
D
Dmitry Derepko, 2019-07-08
@rshruslan

You inherit from and enter into abstract Apithe 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
ifswitch

L
Lander, 2019-07-08
@usdglander

echo $_SERVER['REQUEST_METHOD']; //'GET', 'HEAD', 'POST', 'PUT'.

upd: Soryan, I did not immediately catch up with what exactly is being asked.
That documentation says:
That is, for /api/cars you need to inherit the CarsApi class from the Api class and write its implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question