Skip to content

Formatting

Snappy supports multiple response formats, giving you flexibility in how your API communicates with clients. By default, it can return data in JSON, JSONP, XML, or HTML formats as web page.

Whether you're building a RESTful API, feeding data to a frontend app, or generating lightweight HTML templates, Snappy handles the response formatting seamlessly—without requiring additional configuration.

JSON/JSONP

💡 Usage

returns JSON/JSONP formatted string

php
$this->json( array $result ): string

result

  An associative array/fetched data from database

Example url:
yourdomain.com/examples/10

Output:

json
{
  "response": 200,
  "message": "OK",
  "result": [
    {
      "id": 10,
      "FirstName": "Misha",
      "LastName": "Geroldi",
      "Email": "mgeroldi9@google.co.uk",
      "Status": "INACTIVE"
    }
  ]
}

ℹ️ NOTE

If jsonpcallback is defined in the url as query string then the result will return JSONP.

Example url:
yourdomain.com/examples/10?jsonpcallback=mycallback

Output:

mycallback({"response":200,"message":"OK","result":[{"id":10,"FirstName":"Misha","LastName":"Geroldi","Email":"mgeroldi9@google.co.uk","Status":"INACTIVE"}]})

XML

💡 Usage

returns XML formatted string

php
$this->xml( array $result ): string

result

  An associative array/fetched data from database

Example url:
yourdomain.com/examples/10

Output:

xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <response>200</response>
    <message>OK</message>
    <result>
        <record>
            <id>10</id>
            <FirstName>Misha</FirstName>
            <LastName>Geroldi</LastName>
            <Email>mgeroldi9@google.co.uk</Email>
            <Status>INACTIVE</Status>
        </record>
    </result>
</root>

HTML

💡 Usage

returns HTML formatted string

php
$this->html( string $template, array $result = [] ): string

template

  A file name of your HTML template with mentioning its extension

result (optional)

  An associative array/etched data from database. Leave blank if no data is required for template

ℹ️ NOTE

HTML template location depends on view setting in environment. The default location is under /view directory.

Snappy doesn't have special template syntax on how to handle the $result data, but how to parse it is the same as native PHP when sending values as POST method.

Example url:
yourdomain.com/examples/10

Output:

php
// controller/somecontroller.php

$data = [
  "status" => 200,
  "message" => "OK",
  "result" => [
    "FirstName" => "John",
    "LastName" => "Doe",
    "Email" => "john@doe.com",
    "Status" => "ACTIVE",
  ]
];
$this->html("web-page/view-examples.php", $data["result"]);

// view/web-page/view-examples.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Employee list</title>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <?=$_POST["FirstName"]?>
            <?=$_POST["LastName"]?>
          </td>
          <td><?=$_POST["Email"]?></td>
          <td><?=ucfirst(strtolower($_POST["Status"]))?></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>