1. Go to this page and download the library: Download sailantis/azera-framework 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/ */
sailantis / azera-framework example snippets
Azera\AppContext;
use Azera\Db\Database;
use Azera\Http\Response;
use Azera\Core\Dispatcher;
use Azera\Core\Router;
// Application context holds shared services
// Dispatcher, Controllers, Query Builders and Models access the AppContext
// singleton for database connections, request data, etc. like in this example.
// This allows flexible configuration and easy access to services throughout
// the application without tight coupling.
$ctx = AppContext::instance();
// Register database connection as a lazy service
// The label 'default' is used to identify this connection. The first
// registered connection becomes the default connection. You can register
// multiple connections with different names and roles (e.g. 'read', 'write')
// for read/write splitting. The closure allows for lazy initialization,
// so the connection is only created when first accessed.
$ctx->dbManager()->set('default',
fn() => new Database('mysql:host=localhost;dbname=myapp', 'user', 'pass')
);
// Configure routing
// Define routes with HTTP method, path pattern, and controller action.
// The pattern can
namespace App\Controllers;
use Azera\Core\Controller;
class IndexController extends Controller
{
public function helloAction(string $name): string
{
return "Hello, {$name}!";
}
}
class User extends \Azera\Core\Model
{
public int $id;
public string $username;
public string $email;
public string $status;
}
// Find by primary key
$user = User::find(1);
// Update and save
$user->email = '[email protected]';
$user->save();
// Create new record
$newUser = User::create([
'username' => 'jane',
'email' => '[email protected]',
]);
$newUser->save();
// Delete record
$newUser->delete();
// Count records
$count = User::count(['status' => 'active']);
// Check existence
$exists = User::exists(['email' => '[email protected]']);
// Query with conditions
$users = User::query()
// Column/value style
->where('status', 'active')
// Inline parameters
->where('status = :status', ['status' => 'active'])
->orderBy('created_at DESC')
->limit(10)
->select();
// Insert data
User::query()->insert([
'username' => 'john',
'email' => '[email protected]',
]);
// Update records
User::query()
->where('id', 42)
->update(['status' => 'inactive']);
// Delete records
User::query()->where('status', 'spam')->delete();
use Azera\Validation\Validator;
$v = new Validator($ctx->request()->post());
$v->field('name')->$v->field('role')->default('viewer'); // optional with a default value
if ($v->fails()) {
return Response::json(['errors' => $v->errors()], 422);
}
$data = $v->validated(); // only validated, coerced fields
User::create($data);
$mgr = AppContext::instance()->dbManager();
$mgr->set('write', new Database('mysql:host=master;dbname=app', 'user', 'pass'));
$mgr->set('read', new Database('mysql:host=replica;dbname=app', 'user', 'pass'));
// Models and queries automatically route reads to 'read' role, writes to
// 'write' role. Falls back to default if a specific role is missing
bash
php console.php database migrate latest --direction=down
php console.php help # overview with all tasks and actions
php console.php help database # detail page for one task
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.