api
is a special automatically routed url.
back >
sys >
api
1. This sets /api as an automated api layer interacting with your application
2. It can request a class and method, and returns JSON objects.
3. Classes must declare public $api = ['methodName'] to expose methods.
class controlExample {
public $api = ['getArray','getAssoc'];
public function getArray($get=[],$post=[]) {
return ["success"=>1];
}
}
Methods not listed in $api return {"error":"Method not allowed"}.
POST routes (?post=1) require a CSRF token:
<input type="hidden" name="_csrf" value="<?=$app->get('session')->csrf()?>">
<form action="/api/crud/save?post=1" method="post">
| Name | Description |
|---|---|
| http://yourwebsite.com /api/{class}/{method} | GET request to a class->method returning JSON objects |
| http://yourwebsite.com /api/{class}/{method}?pretty=1 | GET request to a class->method returning JSON objects in pretty print. |
| http://yourwebsite.com /api/{class}/{method}?post=1 | POST request to a class->method |
back > app > modelExample.php
class modelExample {
public $api = ['getArray'];
public function getArray($get=[],$post=[]){
return [1,2,3,4];
}
}
http://website.com/ api/modelExample/getArray
//prints out returned object from $app->example->get_array in JSON format
[1,2,3,4]