PHP code example of kode / framework

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

    

kode / framework example snippets


// app/controllers/HelloController.php
namespace app\controllers;

use Kode\Framework\Http\Controller;

final class HelloController extends Controller
{
    public function say(): array
    {
        $name = $this->input('name', '世界');
        return ['hello' => $name];          // 直接返回数组 → 自动 JSON 化
    }
}

// app/routes.php
use Kode\Http\App;
use app\controllers\HelloController;

return function (App $app): void {
    $app->get('/hello', fn() => resolve(HelloController::class)->say());
};

#[AsCommand(name: 'migrate', description: '执行待运行的数据库迁移', usage: 'migrate {--step=} {--pretend}')]
final class MigrateCommand extends Command
{
    private const OPTS = ['step', 'pretend'];

    protected function handle(): int
    {
        if (($bad = $this->rejectUnknownOptions(self::OPTS)) !== null) {
            return $bad;                       // 未识别的选项 → 打印用法行 + 退出 1
        }

        // 整数用 checkIntOptions,允许小数的秒数用 checkNumOptions
        if (($bad = $this->checkIntOptions(['step'], 1)) !== null) {
            return $bad;                       // --step=abc / 1.5 / 0 / -2 → 报错退出 1
        }
        // ...
    }
}

$pm->residentSlots();  // list<{name, slot, pid_file}> —— 纯计算,不碰磁盘
$pm->slotStates();     // + pid:int|null, alive:bool, started_at:int|null
$pm->signalSlots(\Kode\Process\Signal::USR1);
// → ['signalled' => ['heartbeat:0'], 'skipped' => [{label, reason}], 'failed' => [{label, reason}]]

$pm->start();
// RuntimeException: 常驻进程已在运行,拒绝重复启动:heartbeat:0(pid 4812)、
//                   queue:0(pid 4813)、queue:1(pid 4814)。
//                   需要换 worker 注册表请先 stop 再 start;只想重载代码请发 USR1(reload)。

} catch (\Throwable $e) {
    logger()->warning('查询调度统计失败:' . $e->getMessage());

    return $taskName !== null ? ['total' => 0, …, 'success_rate' => 100.0] : [];
}

} catch (\Throwable $e) {
    logger()->warning("查询调度执行历史失败({$taskName}):" . $e->getMessage());

    return [];
}
bash
php kode make:controller Dashboard --app=admin   # → app/admin/controllers/DashboardController.php