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, dynamic routing with variables and different response types.

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 Middleware With Dynamic Routes.

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

Sessions are started by default in the Tina4\Auth constructor.

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="/process-form">
    {{ "emailForm" | formToken }}
    <input name="email">
    <button>Save</button>
</form>

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

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

More details on posting form data, how to secure your routes, working with Tina4 tokens, uploading files, how to handle errors and a full login example.

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, Annotations and Usage.

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

Returning a single row is as easy as

php
$dataResult = $DBA->fetchOne("select * from test_record order by id");

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. This command will create a migration file in the migrations folder. Just add your sql.

bash

composer migrate:create my-first-migration

A number of migration creations can be made before executing the migrations. Once all creations are finished, just run them.

bash

composer migrate

Alternatively you can spin up the webserver and do the same from the browser.

http://localhost:7145/migrate/create

http://localhost:7145/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

Tina4 allows testing to be added to functions without having to set up a test suite.

php
    /**
     * @tests Cris
     * assert(2,5)==7,"2+5 not equal 7"
     */
    public function addTwoNumbers($number1, $number2)
    {
        return $number1 + $number2;
    }

After making changes you can run the tests

bash

composer test

Limitations and

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

Declare your WSDL definition

php
class Calculator extends \Tina4\WSDL {
    protected array $returnSchemas = [
        "Add" => ["Result" => "int"],
        "SumList" => [
            "Numbers" => "array<int>",
            "Total" => "int",
            "Error" => "?string"
        ]
    ];

    public function Add(int $a, int $b): array {
        return ["Result" => $a + $b];
    }

    /**
     * @param int[] $Numbers
     */
    public function SumList(array $Numbers): array {
        return [
            "Numbers" => $Numbers,
            "Total" => array_sum($Numbers),
            "Error" => null
        ];
    }
}

Add your WSDL routes

php
\Tina4\Any::add("/calculator", function (\Tina4\Request $request, \Tina4\Response $response) {
    $calculator = new Calculator($request);
    $handle = $calculator->handle();
    return $response($handle, HTTP_OK, APPLICATION_XML);
});

More Details are available for WSDL