Listing Records
The get_all() endpoint is our simplest API method. It retrieves all countries from the database, orders them alphabetically, and returns them as a JSON array. There is no pagination in this version - every matching record is returned.
The Model Method
The model method is straightforward: a single SQL query with no parameters. It returns an array of objects, which json_encode() will serialise into a JSON array automatically.
public function get_all(): array {
$sql = 'SELECT id, country_title, country_code
FROM countries
ORDER BY country_title ASC';
return $this->db->query($sql, 'object');
}
We explicitly list the columns we want (id, country_title, country_code) rather than using SELECT *. This keeps the API response clean and predictable - no hidden fields, no extra data.
The Controller Method
public function get_all(): void {
$this->authenticate();
$rows = $this->model->get_all();
if (empty($rows)) {
http_response_code(200);
echo json_encode([]);
return;
}
http_response_code(200);
echo json_encode($rows);
$this->log_request('get_all');
}The flow is simple: authenticate, fetch, check for empty results, encode, and log. No views, no templates, no HTML.
Testing with cURL
Once the controller is in place, you can test it from the command line. Replace your-token with an actual Trongate token:
curl -H "Trongatetoken: your-token" \
http://localhost/countries_api/get_allThe response will look like this:
[
{"id":1,"country_title":"Afghanistan","country_code":"AF"},
{"id":2,"country_title":"Albania","country_code":"AL"},
{"id":3,"country_title":"Algeria","country_code":"DZ"},
...
]Testing Without a Token
If you omit the token or provide an invalid one, the authenticate() method will return a 401 response:
curl -i http://localhost/countries_api/get_allHTTP/1.1 401 Unauthorized
Content-Type: text/html; charset=UTF-8
{"error":"Unauthorized. A valid API token is required."}
Note the -i flag in the cURL command - this shows the HTTP response headers, including the status code. This is useful when testing API behavior.
Testing with a Browser
If you are logged into your Trongate application in the same browser, the token is stored in your session. You can simply visit:
http://localhost/countries_api/get_allThe browser will display the raw JSON. This is a convenient way to test during development, but remember: real API clients will send the token in a header, not via a browser session.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.