PHP code example of skim / framework

1. Go to this page and download the library: Download skim/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/ */

    

skim / framework example snippets


// config/app.php
return [
    'name'  => env('APP_NAME', 'SKIM App'),
    'debug' => env('APP_DEBUG', false),
];

// Read anywhere:
config('app.name');
config('db.default.host');
env('DB_HOST', 'localhost');

// routes.php
$app->router->get('/', [HomeController::class, 'index']);
$app->router->post('/users', [UserController::class, 'store']);

// Route params
$app->router->get('/users/@id:int', [UserController::class, 'show']);
// @id       → any string
// @id:int   → digits only
// @slug:str → letters, digits, dashes

// Named routes
$app->router->get('/users/@id', [UserController::class, 'show'])->name('user.show');
route('user.show', ['id' => 5]); // → /users/5

// Groups with middleware
$app->router->group('/api', function(Router $r) {
    $r->get('/users', [api\UserController::class, 'index']);
}, middleware: [AuthMiddleware::class]);

// %placeholders% are removed silently if their keys are absent — no string concatenation needed
Db::all('SELECT * FROM users %where% %limit%', [
    'where'   => ['status = :status'],
    ':status' => 'active',
    'limit'   => 20,
]);

Db::row('SELECT * FROM users WHERE id = :id', [':id' => 5]);
Db::val('SELECT COUNT(*) FROM users');

Db::transaction(function() {
    Db::query('UPDATE accounts %set% WHERE id = :id', ['set' => ['balance' => 100], ':id' => 1]);
    Db::query('UPDATE accounts %set% WHERE id = :id', ['set' => ['balance' => 200], ':id' => 2]);
});

class User extends \Skim\Db\Model {
    protected static string $table   = 'users';
    protected static array  $guarded = ['id', 'created_at'];
    protected static array  $casts   = ['age' => 'int', 'is_active' => 'bool'];
}

User::find(1);                            // Model|null
User::findOrFail(1);                    // Model|NotFoundException
User::where(['status' => 'active'])->limit(10)->all();
User::create(['name' => 'John', 'email' => '[email protected]']);

$user->name = 'Jane';
$user->save();    // INSERT if no id, UPDATE only changed columns if id set

$user->delete();

// config/db.php — add as many named connections as needed
'alt' => [
    'driver'   => 'pgsql',
    'host'     => env('ALT_DB_HOST', 'pgsql'),
    // ...
];

// Use the connection name on any query
Db::all('SELECT * FROM reports', [], connection: 'alt');

Cache::set('user:1', $user, 3600);
Cache::get('user:1');
Cache::remember('user:1', 3600, fn() => User::find(1));   // get or compute+cache
Cache::delete('user:1');
Cache::flush('user:');         // remove all keys starting with 'user:'
Cache::tags(['users'])->flush();  // Redis only — tag-scoped invalidation

// Controller
return $res->view('users/show', ['user' => $user]);
return $res->fragment('users/show', ['user' => $user], 'user-card');  // partial only
return $res->smartView('users/show', ['user' => $user], $req);        // auto full/partial

// Template: app/views/users/show.php
<h1><?= e($user->name) 

class AuthMiddleware implements Middleware {
    public function handle(Request $req, Response $res, callable $next): mixed {
        if (!Session::has('user_id')) {
            return $res->status(401)->json(['error' => 'Unauthorized']);
        }
        return $next($req, $res);
    }
}

$app->use(Cors::class);                           // global — every request
$route->middleware([AuthMiddleware::class]);      // per-route

class GreetCommand extends Command {
    public function handle(): int {
        $name = $this->arg(0, 'World');
        $this->info("Hello {$name}!");
        return 0;
    }
}

// Register in config/app.php:
'commands' => ['greet' => GreetCommand::class]

// Run:
// php skim greet John

// Request testing without HTTP
$req = Request::make('POST', '/users', headers: ['Content-Type' => 'application/json'], raw_body: '{"name":"John"}');
$res = new Response();

// Fake HTTP client
$http = Client::fake(['GET https://api.example.com/users' => ['status' => 200, 'body' => []]]);
$resp = $http->get('https://api.example.com/users');
$http->assertSent('GET', 'users');
bash
php skim cache:build
bash
php skim install        # fills it automatically
# or manually:
php -r "echo 'base64:'.base64_encode(random_bytes(32)).PHP_EOL;"
bash
php bin/skim list --agent
php bin/skim migrate:status --agent