Skip to content

Tina4 PHP – Quick Reference

Installation

bash
composer require tina4stack/tina4php
composer exec tina4 initialize:run
composer start

More details around project setup and some customizations.

Static Websites

Put .html or .twig files in ./src/templates • assets in ./public

twig
<!-- src/templates/index.twig -->
<h1>Hello Static World</h1>

More details on static website routing.

Basic Routing

php
\Tina4\Get::add("/", function (\Tina4\Response $response) {
    return $response("<h1>Hello Tina4 PHP</h1>");
});

// Post requires a formToken or Bearer auth
\Tina4\Post::add("/api", function (\Tina4\Request $request, \Tina4\Response $response) {
    return $response(["data" => $request->params]);
});

// redirect after post
\Tina4\Post::add("/register", function (\Tina4\Request $request, \Tina4\Response $response) {
    \Tina4\redirect("/welcome");
});

Follow the links for , this basic routing and dynamic routing with variables.

Middleware

php
// Declare the middleware 
\Tina4\Middleware::add("MyMiddleware", function (\Tina4\Response $response, \Tina4\Request &$request) {

    return $response("This is not my middleware");
});

// The middleware will intercept the route, which will actually never fire in this design
\Tina4\Get::add("/my-route", function (\Tina4\Response $response, \Tina4\Request $request) {

    return $response("This is my route");
})::middleware(["MyMiddleware"]);

Follow the links for more on Middleware Declaration, Linking to Routes, Middleware Chaining and Variable Modification

Template Rendering

Put .twig files in ./src/templates • assets in ./public. Render the templates passing data in an array.

twig
<!-- src/templates/hello.twig -->
<h1>Hello {{name}}</h1>
php
\Tina4\Get("/", function (\Tina4\Request $request, \Tina4\Response $response) {

    return $response(\Tina4\renderTemplate("hello.twig", ["name" => "World!"]));
});

Sessions

The default session handling is done at the time of authentication

SCSS Stylesheets

Drop in ./src/scss then default.css is auto-compiled to ./public/css

scss
// src/scss/main.scss
$primary: #2c3e50;
body {
  background: $primary;
  color: white;
}

More details on css and scss.

Environments

Default development environment in .env

[Project Settings]
VERSION=1.0.0
TINA4_DEBUG=true
TINA4_DEBUG_LEVEL=[TINA4_LOG_ALL]
TINA4_CACHE_ON=false
[Open API]
SWAGGER_TITLE=Tina4 Project
SWAGGER_DESCRIPTION=Edit your .env file to change this description
SWAGGER_VERSION=1.0.0

Environment variables are available through the Environment superglobal variable.

php
$data = $_ENV["SWAGGER_TITLE"];

Authentication

All POST routes are naturally secured. GET routes can be secured through php annotations

php
/**
 * @secure
 */
\Tina4\Get::add("/my-route", function(\Tina4\Response $response) {
   
    return $response("This route is protected");
});

A valid bearer token or Tina4 formed JWT token are valid authorizations

HTML Forms and Tokens

Form tokens can be added using a Tina4 twig filter

twig
<form method="POST" action="submit">
    {{ "emailForm" | formToken | raw }}
    <input name="email">
    <button>Save</button>
</form>

Renders out this form with "emailForm" sent via the JWT payload

html
<form method="POST" action="submit">
    <input type="hidden" name="formToken" value="ey...">
    <input name="email">
    <button>Save</button>
</form>

AJAX and tina4helper.js

Tina4 ships with a small javascript library, in the bin folder, to assist with the heavy lifting of ajax calls.

More details on available features.

OpenAPI and Swagger UI

Swagger is built into Tina4 and found at /swagger. Adding the @description annotation will include the route into swagger.

php
/**
 * @description Returns all users
 */
\Tina4\Get("/users", function (\Tina4\Response $response) {

    return $response((new User())->select("*"));
});

Follow the links for more on Configuration, Usage and Annotations.

Databases

Each database module implements the Database interface and needs to be included into composer, depending on which Database has been selected.

bash

composer require tina4stack/tina4php-sqlite3

The initial database connection in index.php might differ due to database selected.

php
//Initialize Sqlite Database Connection
global $DBA;
$DBA = new \Tina4\DataSQLite3("database/myDatabase.db", "username", "my-password", "d/m/Y");

Follow the links for more on Available Connections, Core Methods, Usage and Full transaction control.

Database Results

Database objects all return a DataResult object, which can then be returned in a number of formats.

php
// fetch($sql, $noOfRecords, $offset)
$dataResult = $DBA->fetch("select * from test_record order by id", 3, 1);

$list = $dataResult->asArray();
$array = $dataResult->asObject();

Looking at detailed Usage will improve deeper understanding.

Migrations

Migrations are available as cli commands. Any valid SQL is usually acceptable

bash

composer migrate:create

or as routes. Any valid SQL is usually acceptable

/migrate/create

A number of migration creations can be made before executing the migrations.

bash

composer migrate

or as a route

/migrate

Migrations do have some limitations and considerations when used extensively.

ORM

Once you have run your migrations, creating the tables, ORM makes database interactions seamless.

php
class User extends Tina4\ORM
{
    public $tableName = 'user';
    
    public $id;
    public $email;
}

$user = new User(["email" => "my-email@email.com"]);
$user->save();
$user = (new User())->load("id = ?", 1);

ORM functionality is quite extensive and needs more study of the Advanced Detail to get the full value from ORM.

CRUD

With a single line of code, Tina 4 can generate a fully functional CRUD system, screens and all.

php
(new User())->generateCrud("/my-crud-templates")

More details on how CRUD works, where it puts the generated files is worth some investigation.

Consuming REST APIs

Getting data from a public api is as simple as one line of code.

php
$api = (new \Tina4\Api())->sendRequest("https://api.example.com", "GET");

More details are available on sending a post data body, authorizations and other finer controls of sending api requests.

Inline Testing

Documentation coming soon . . .

Services

Create the required process

php
class MyProcess extends \Tina4\Process
{
    public function canRun(): bool
    {
        // Include any selection criteria you need, or just return true
        return true;
    }
    
    public function run(): void
    {
        // Do whatever you want here
    }
}

Add the process to the service

php
    $service = (new \Tina4\Service());
    $service->addProcess(new MyProcess("Unique Process Name"));

Create the service on the server by creating and registering an appropriate script.

There are a number of special cases that Need Investigating for getting the full value out of services, and should be studied in conjunction with threads.

Threads

Create the thread code as required

php
Tina4\Thread::addTrigger('myNewProcess', function () {
    // Do whatever you want to do here
});

Call the thread as required

php
// Starts a new php thread running the code as declared above
Tina4\Thread::trigger('myNewProcess');

Please read More Details on Threads, their restrictions and usage ideas.

Queues

Services and Threads together can be used to replicate queues, but stand alone queues are not implemented in Tina4 Php.

WSDL

This is not available in Tina4 Php.