PHP code example of duxweb / dux-lite

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

    

duxweb / dux-lite example snippets



// app/Web/App.php - Web 模块注册类
namespace App\Web;

use Core\App\AppExtend;
use Core\Bootstrap;

class App extends AppExtend
{
    public function init(Bootstrap $bootstrap): void
    {
        // 模块初始化逻辑
    }

    public function register(Bootstrap $bootstrap): void
    {
        // 注册服务和组件
    }

    public function boot(Bootstrap $bootstrap): void
    {
        // 模块启动逻辑
    }
}


// 传统路由定义
use Core\App;

App::route()->get('/users', [UserController::class, 'index']);
App::route()->post('/users', [UserController::class, 'store']);
App::route()->get('/users/{id}', [UserController::class, 'show']);

// 属性注解路由
#[Route('/api/users', methods: ['GET'])]
#[Route('/api/users', methods: ['POST'], name: 'users.store')]
class UserController
{
    public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $users = User::paginate(15);
        return response()->json($users);
    }

    public function store(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $data = $request->getParsedBody();
        $user = User::create($data);
        return response()->json($user, 201);
    }
}


// 查询构造器
$users = App::db()->table('users')
    ->where('status', 1)
    ->orderBy('created_at', 'desc')
    ->paginate(15);

// Eloquent 模型
class User extends Model
{
    protected $fillable = ['name', 'email', 'password'];
    protected $hidden = ['password'];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

// 模型操作
$user = User::create([
    'name' => '张三',
    'email' => '[email protected]',
    'password' => password_hash('123456', PASSWORD_DEFAULT)
]);

$users = User::with('posts')->where('status', 1)->get();


// 基础缓存操作
$cache = App::cache();

// 设置缓存
$cache->set('user:1', $userData, 3600);

// 获取缓存
$userData = $cache->get('user:1');

// 缓存闭包
$users = $cache->remember('users:active', 3600, function() {
    return User::where('status', 1)->get();
});

// Redis 缓存
$redis = App::cache('redis');
$redis->set('session:' . $sessionId, $sessionData, 1800);


// 定义队列任务
namespace App\Common\Jobs;

use Core\Queue\QueueMessage;

class SendEmailJob extends QueueMessage
{
    public function __construct(
        private string $to,
        private string $subject,
        private string $content
    ) {}

    public function handle(): void
    {
        // 发送邮件逻辑
        $mailer = App::di()->get('mailer');
        $mailer->send($this->to, $this->subject, $this->content);
    }

    public function failed(\Throwable $exception): void
    {
        // 任务失败处理
        App::log()->error('邮件发送失败', [
            'to' => $this->to,
            'error' => $exception->getMessage()
        ]);
    }
}

// 分发任务
App::queue()->push(new SendEmailJob(
    '[email protected]',
    '欢迎注册',
    '欢迎使用 DuxLite 框架!'
));

// 延迟分发
App::queue()->later(300, new SendEmailJob(...));


// 定义事件监听器
#[Listener('user.created')]
class UserCreatedListener
{
    public function handle($event): void
    {
        $user = $event['user'];

        // 发送欢迎邮件
        App::queue()->push(new SendWelcomeEmailJob($user->email));

        // 记录日志
        App::log()->info('新用户注册', ['user_id' => $user->id]);
    }
}

// 触发事件
App::event()->dispatch('user.created', ['user' => $user]);
bash
# 使用 PHP 内置服务器
cd public
php -S localhost:8000

# 访问 http://localhost:8000