PHP code example of v10086 / plan9

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

    

v10086 / plan9 example snippets


return [
    '/' => 'controller/Index@index',        // 首页
    '/api/users' => 'controller/User@list', // 用户列表接口
];

// app/controller/Index.php
namespace controller;

class Index {
    public function index() {
        return '欢迎使用 Plan9 框架!';
    }
}

// 返回 HTML
public function html() {
    return '<h1>Hello Plan9</h1>';
}

// 返回 JSON
public function json() {
    return [
        'code' => 200,
        'message' => 'success',
        'data' => ['name' => 'Plan9']
    ];
}

return [
    'mysql' => [
        'default' => [
            'dsn' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8mb4',
            'user' => 'root',
            'password' => ''
        ]
    ]
];

// 查询数据
$users = dbexec('SELECT * FROM users WHERE status = ?', [1]);

// 插入数据
dbexec('INSERT INTO users (name, email) VALUES (?, ?)', ['张三', '[email protected]']);

// 更新数据
dbexec('UPDATE users SET status = ? WHERE id = ?', [0, 1]);

// 删除数据
dbexec('DELETE FROM users WHERE id = ?', [1]);

// 获取最后插入的 ID
$id = dbexec('lastInsertId');

try {
    // 开始事务
    $db = dbnew(config('database.mysql.default'));
    $db->beginTransaction();
    
    // 执行多个操作
    dbexec('INSERT INTO orders (...) VALUES (...)', [...], $db);
    dbexec('UPDATE inventory SET quantity = quantity - 1 WHERE id = ?', [1], $db);
    
    // 提交事务
    $db->commit();
} catch (\Throwable $e) {
    // 回滚事务
    $db->rollBack();
    error_log($e->getMessage());
}

$dbConfig = [
    'host' => '127.0.0.1',
    'user' => 'root',
    'password' => '',
    'database' => 'test'
];

$sqls = [
    'user' => 'SELECT * FROM users WHERE id = ?',
    'order' => 'SELECT * FROM orders WHERE user_id = ?',
    'product' => 'SELECT * FROM products LIMIT 10'
];

$results = asyncMysqliBatch($dbConfig, $sqls, 3);

// 获取整个数据库配置
$dbConfig = config('database');

// 获取默认数据库配置
$defaultDb = config('database.mysql.default');

// 获取嵌套配置
$dbHost = config('database.mysql.default.dsn');

// 发送 GET 请求
$response = http('https://api.example.com/users');

// 发送 POST 请求
$response = http('https://api.example.com/users', [
    'method' => 'POST',
    'body' => ['name' => 'Test', 'email' => '[email protected]'],
    'headers' => ['Content-Type' => 'application/json']
]);

// 保存文件
fileSave('/path/to/file.txt', 'Hello Plan9');

// 加密数据
$encrypted = encrypt('secret data', 'your-key');

// 解密数据
$decrypted = decrypt($encrypted, 'your-key');

plan9/
├── app/
│   ├── bootstrap.php      # 框架初始化
│   └── controller/        # 控制器目录
├── config/               # 配置文件
│   ├── database.php      # 数据库配置
│   └── route.php         # 路由配置
├── function/             # 全局工具函数
│   ├── core.php          # 核心功能
│   ├── db.php            # 数据库操作
│   └── helper.php        # 辅助函数
├── public/               # Web 入口
│   └── index.php         # 单文件入口
├── composer.json         # Composer 配置
├── CHANGELOG.md          # 变更记录
└── README.md             # 项目文档
nginx
server {
    listen 80;
    server_name example.com;
    root /path/to/plan9/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
bash
# 安装 PHP-FPM
sudo apt-get install php7.4-fpm

# 启动 PHP-FPM
sudo systemctl start php7.4-fpm