Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
dab07e55ec | |||
a10e864ba3 | |||
9d45bb5ceb | |||
f1e6e92f46 |
60
README.md
60
README.md
@ -2,6 +2,44 @@
|
|||||||
|
|
||||||
A set of tools to simplify the work of creating backend APIs for your frontend projects. Includes tutorials, patterns and practical examples for creating projects based on REST APIs.
|
A set of tools to simplify the work of creating backend APIs for your frontend projects. Includes tutorials, patterns and practical examples for creating projects based on REST APIs.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Download source code and add to your project
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/APIlite/src/APIlite.php';
|
||||||
|
```
|
||||||
|
|
||||||
|
or use composer
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require tpsoft/apilite
|
||||||
|
```
|
||||||
|
|
||||||
|
and add to your project
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
class YourAPI extends \TPsoft\APIlite\APIlite {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* My first method for greetings.
|
||||||
|
*
|
||||||
|
* @param string $name Your name
|
||||||
|
* @param int $age Your age
|
||||||
|
* @return string Greetings
|
||||||
|
*/
|
||||||
|
public function myFirstMethod(string $name, int $age): string
|
||||||
|
{
|
||||||
|
return 'Hi, I`m '.$name.' and I`m '.$age.' years old.';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
For example, we create an API for calculator. So we create class `APIcalculator` and store in file `test/APIcalculator.php` where we defined each actions for API as public method.
|
For example, we create an API for calculator. So we create class `APIcalculator` and store in file `test/APIcalculator.php` where we defined each actions for API as public method.
|
||||||
@ -199,10 +237,10 @@ To connect to the API from TypeScript (e.g. Vue application) it is possible to d
|
|||||||
* Generated by APIlite
|
* Generated by APIlite
|
||||||
* https://gitea.tpsoft.org/TPsoft.org/APIlite
|
* https://gitea.tpsoft.org/TPsoft.org/APIlite
|
||||||
*
|
*
|
||||||
* 2025-05-28 15:44:07 */
|
* 2025-06-12 06:24:33 */
|
||||||
|
|
||||||
export const backend = {
|
class APIcalculator {
|
||||||
endpont: window.location.origin + "APIcalculator.php",
|
endpont = "http://";
|
||||||
|
|
||||||
/* ----------------------------------------------------
|
/* ----------------------------------------------------
|
||||||
* General API call
|
* General API call
|
||||||
@ -227,7 +265,7 @@ export const backend = {
|
|||||||
});
|
});
|
||||||
xhttp.open('POST', this.endpont + '?action=' + method);
|
xhttp.open('POST', this.endpont + '?action=' + method);
|
||||||
xhttp.send(form_data);
|
xhttp.send(form_data);
|
||||||
},
|
}
|
||||||
|
|
||||||
callPromise(method, data) {
|
callPromise(method, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -239,34 +277,34 @@ export const backend = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------
|
/* ----------------------------------------------------
|
||||||
* API actions
|
* API actions
|
||||||
*/
|
*/
|
||||||
help() {
|
help() {
|
||||||
return this.callPromise('__HELP__', {});
|
return this.callPromise('__HELP__', {});
|
||||||
},
|
}
|
||||||
|
|
||||||
add(a, b) {
|
add(a, b) {
|
||||||
return this.callPromise('add', {a: a, b: b});
|
return this.callPromise('add', {a: a, b: b});
|
||||||
},
|
}
|
||||||
|
|
||||||
subtract(a, b) {
|
subtract(a, b) {
|
||||||
return this.callPromise('subtract', {a: a, b: b});
|
return this.callPromise('subtract', {a: a, b: b});
|
||||||
},
|
}
|
||||||
|
|
||||||
multiply(a, b) {
|
multiply(a, b) {
|
||||||
return this.callPromise('multiply', {a: a, b: b});
|
return this.callPromise('multiply', {a: a, b: b});
|
||||||
},
|
}
|
||||||
|
|
||||||
divide(a, b) {
|
divide(a, b) {
|
||||||
return this.callPromise('divide', {a: a, b: b});
|
return this.callPromise('divide', {a: a, b: b});
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default new BackendAPI();
|
||||||
```
|
```
|
||||||
|
|
||||||
These outputs can also be generated in the command line as follows
|
These outputs can also be generated in the command line as follows
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class <?php echo $this->apiName; ?> {
|
class <?php echo $this->apiName; ?> {
|
||||||
endpont = "<?php echo $this->endpoint; ?>";
|
endpoint = <?php echo sprintf(substr($this->endpoint, 0, 4) == 'http' ? '"%s"' : '%s', $this->endpoint); ?>;
|
||||||
|
|
||||||
/* ----------------------------------------------------
|
/* ----------------------------------------------------
|
||||||
* General API call
|
* General API call
|
||||||
@ -29,7 +29,7 @@ class <?php echo $this->apiName; ?> {
|
|||||||
if (typeof val == 'object') val = JSON.stringify(val);
|
if (typeof val == 'object') val = JSON.stringify(val);
|
||||||
form_data.append(key, val);
|
form_data.append(key, val);
|
||||||
});
|
});
|
||||||
xhttp.open('POST', this.endpont + '?action=' + method);
|
xhttp.open('POST', this.endpoint + '?action=' + method);
|
||||||
xhttp.send(form_data);
|
xhttp.send(form_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user