PHP code example of ivupcn / restina

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

    

ivupcn / restina example snippets

bash
php restina queue:consume --queue=default --max-jobs=1000 --memory=256
bash
php restina queue:work --queue=default --workers=1 --daemon
bash
php restina scheduler:run

namespace App\Controllers;

use Restina\attribute\Route;
use Restina\attribute\Params;
use Restina\attribute\enum\FieldType;

class HelloController
{
    #[Route(methods: ['GET'], path: '/hello', jwt: false, permission: false)]
    #[Params(field: 'name', title: '姓名', type: FieldType::STRING, rules: '

// public/index.php
p::init()->boot()->run()->end();
~~~

> 如果你没有特殊的自定义需求,无需对入口文件做任何的更改。
> 如果需要自定义入口行为,可以在调用`App::init()`之前添加预处理逻辑。
> 入口文件位置的设计是为了让应用部署更安全,请尽量遵循public目录为唯一的web可访问目录,其他的文件都可以放到非WEB访问目录下面。

## URL访问

Restina的URL访问受路由影响。

框架扫描每个类的每个方法,如果方法标记了 `#[Route()]`,将被自动添加为路由。

~~~
class DemoController
{
     #[Route(methods: ['GET'], path: '/demo/getUsers', code: 'demo.getUsers', permission: false, jwt: false, autoRefreshToken: true)]
    public function getUsers(int $page = 1, int $limit = 10, string $sort = 'id', string $search = '')

namespace App\Jobs;

use Restina\queue\Job;

class SendEmailJob extends Job
{
    public function __construct(array $data = [])
    {
        parent::__construct(self::class, $data);
    }

    public function handle(array $data): bool
    {
        // 执行任务逻辑
        // 发送邮件等操作
        return true;
    }

// app/scheduler.php

return [
    'daily-backup' => [
        'cron' => '0 2 * * *',
        'name' => 'daily-backup',
        'desc' => '每日备份',
        'class' => App\Tasks\BackupTask::class,
        'method' => 'backup',
        'type' => 'method'
    ],
    'weekly-report' => [
        'cron' => '0 0 * * 0',
        'name' => 'weekly-report',
        'desc' => '每周报告',
        'class' => App\Tasks\ReportTask::class,
        'method' => 'generateWeeklyReport',
        'type' => 'method'
    ]
];
~~~

### 启动定时任务调度器

使用命令行启动调度器:



或者在系统 crontab 中添加以下条目,使调度器每分钟运行一次:



### Cron 表达式说明

定时任务使用标准 Cron 表达式:



常用示例:
- `* * * * *`:每分钟执行
- `0 * * * *`:每小时执行
- `0 2 * * *`:每天凌昨2点执行
- `0 0 * * 0`:每周日凌晨执行
- `0 22 * * 1-5`:工作日晚上10点执行

### 动态添加任务

可以通过代码动态添加定时任务:

~~~php
use Restina\Scheduler;

class TaskManager
{
    #[Inject]
    private Scheduler $scheduler;
    
    public function registerTasks(): void
    {
        $this->scheduler->addTask(
            name: 'custom-task',
            cron: '*/5 * * * *',
            callback: function() {
                // 任务逻辑
            },
            description: '每5分钟执行一次'
        );
    }

namespace App\Commands;

use Restina\App;
use Restina\console\Command;

class MigrateCommand extends Command
{
    protected string $signature = 'migrate {--force : 强制执行}';
    protected string $description = '执行数据库迁移';

    protected function configure(): void
    {
        // 可以在这里动态修改 signature
        $this->signature = 'migrate {--force : 强制执行} {--seed : 是否填充数据}';
    }

    public function handle(App $app): int
    {
        if ($this->hasOption('force')) {
            $this->info('强制模式已启用');
        }
        
        // 迁移逻辑...
        
        $this->success('迁移完成');
        return 0;
    }

namespace App\Controllers;

use Restina\attribute\Route;
use Restina\attribute\Params;
use Restina\attribute\Inject;
use Restina\attribute\enum\FieldType;
use Restina\Request;
use Restina\Db;
use Restina\Jwt;

class UserController
{
    // 属性注入
    #[Inject]
    private Db $db;

    #[Inject]
    private Jwt $jwt;

    /**
     * 获取用户列表
     */
    #[Route(methods: ['GET'], path: '/users', code: 'user.list')]
    #[Params(field: 'page', title: '页码', type: FieldType::INTEGER, default: 1, rules: 'min:1|integer')]
    #[Params(field: 'limit', title: '每页数量', type: FieldType::INTEGER, default: 10, rules: 'min:1|max:100|integer')]
    public function list(int $page = 1, int $limit = 10): array
    {
        $offset = ($page - 1) * $limit;
        $users = $this->db->table('users')
            ->offset($offset)
            ->limit($limit)
            ->get();

        return ['code' => 0, 'data' => $users, 'page' => $page, 'limit' => $limit];
    }

    /**
     * 获取单个用户
     */
    #[Route(methods: ['GET'], path: '/users/{id}', code: 'user.show')]
    #[Params(field: 'id', title: '用户ID', type: FieldType::INTEGER, rules: 'h: '/auth/login', code: 'auth.login', jwt: false, permission: false)]
    #[Params(field: 'email', title: '邮箱', type: FieldType::STRING, rules: '