PHP code example of tina4stack / tina4php

1. Go to this page and download the library: Download tina4stack/tina4php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tina4stack / tina4php example snippets


use Tina4\Router;
use Tina4\Request;
use Tina4\Response;

Router::get("/api/items", function (Request $request, Response $response) {
    return $response(["items" => []], HTTP_OK);
});

Router::post("/api/webhook", function (Request $request, Response $response) {
    return $response(["ok" => true], HTTP_OK);
})->noAuth();

class User extends \Tina4\ORM
{
    public $tableName = "users";
    public $primaryKey = "id";
    public $softDelete = true;

    public $id;
    public $name;
    public $email;

    public $hasMany = [["Order" => "userId"]];
    public $hasOne = [["Profile" => "userId"]];
    public $belongsTo = [["Customer" => "customerId"]];
}

$user = new User($request);
$user->save();
$user->load("email = '[email protected]'");
$user->delete();

$users = (new User())->select("*", 100)->asArray();

use Tina4\Database\Database;

$db = Database::create('sqlite:///app.db');
$db = Database::create('postgres://localhost:5432/mydb', username: 'user', password: 'pass');
$db = Database::create('mysql://localhost:3306/mydb', username: 'root', password: 'secret');
$db = Database::create('mssql://localhost:1433/mydb', username: 'sa', password: 'pass');
$db = Database::create('firebird://localhost:3050/path/to/db.fdb', username: 'SYSDBA', password: 'masterkey');

$result = $db->fetch("SELECT * FROM users WHERE age > ?", [18]);

$token = \Tina4\Auth::getToken(["user_id" => 42], "your-secret");
$payload = \Tina4\Auth::validToken($token, "your-secret");
$claims = \Tina4\Auth::getPayload($token);

// File, Redis, Valkey, MongoDB, or database-backed sessions
$_SESSION["user_id"] = 42;
$userId = $_SESSION["user_id"];

use Tina4\Queue;

$queue = new Queue();
$queue->push("email.send", ["to" => "[email protected]", "subject" => "Welcome"]);
$queue->push("report.generate", ["id" => 42], priority: 10, delay: 60);

// Process jobs
$queue->process("email.send", function ($job) {
    sendEmail($job["to"], $job["subject"]);
});

// Auto-schema from ORM models -- no configuration needed
// GET  /graphql  -> GraphiQL IDE
// POST /graphql  -> Execute queries

// Query:  { users { id, name, email } }
// Mutation support via ORM save/delete

use Tina4\WebSocket;

$ws = new WebSocket('0.0.0.0', 8080);

$ws->on('message', function ($connection, $message) {
    $connection->send("Echo: " . $message);
});

$ws->on('open', function ($connection) {
    $connection->send("Welcome!");
});

$ws->start();

/**
 * @description Get all users
 * @tags Users
 */
Router::get("/api/users", function (Request $request, Response $response) {
    return $response((new User())->select("*", 100)->asArray(), HTTP_OK);
});

use Tina4\Events;

Events::on("user.created", function ($user) {
    sendWelcomeEmail($user);
}, priority: 10);

Events::once("app.boot", function () {
    warmCaches();
});

Events::emit("user.created", $userData);
Events::off("user.created");

// Auto WSDL generation from annotated service classes
// Endpoint: /wsdl

$api = new \Tina4\Api("https://api.example.com", "Bearer xyz");
$result = $api->sendRequest("/users/42");
$result = $api->sendRequest("/users", "POST", ["name" => "Alice"]);

use Tina4\FakeData;

$fake = new FakeData();
$fake->name();        // "Alice Johnson"
$fake->email();       // "[email protected]"
$fake->phone();       // "+1-555-0123"
$fake->address();     // "123 Main St, Springfield"
$fake->paragraph();   // Lorem ipsum...

use Tina4\Messenger;

$messenger = new Messenger();
$messenger->sendEmail(
    "[email protected]",
    "Subject Line",
    "<h1>Hello</h1><p>Message body</p>"
);

$container = new \Tina4\Container();
$container->singleton('db', fn() => Database::create(getenv('DB_URL')));
$container->register('mailer', fn() => new MailService());

$db = $container->get('db');       // same instance every time
$mailer = $container->get('mailer'); // new instance each time

/**
 * @tests
 *   assert (1,1) === 2, "1 + 1 = 2"
 *   assert is_integer(1,1) === true, "This should be an integer"
 */
function add($a, $b) {
    return $a + $b;
}

use Tina4\I18n;

$i18n = new I18n('en');
echo $i18n->translate("welcome.message", ["name" => "Alice"]);

extract(\Tina4\HtmlElement::helpers());
echo $_div(["class" => "card"],
    $_h2("Title"),
    $_p("Content")
);
bash
# 1. Install
composer ?php rue' > .env

# 4. Run (no file watching in this mode)
composer start
bash
cd example
bash setup.sh          # macOS/Linux
# or: setup.bat        # Windows
php index.php