lozitskiys / verse
基于装饰器的微型PHP框架
v0.1.4
2024-03-07 13:01 UTC
Requires
- php: ^8.0
- ext-json: *
- ext-openssl: *
- ext-pdo: *
- psr/container: ^1.0
- symfony/yaml: ^4.2
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-07 14:26:17 UTC
README
index.php 示例
<?php /** @var \Verse\Env $env */ $env = require_once __DIR__ . '/../env.php'; $user = new CurrentUser($env->pdo()); $app = // App decorator new AppLocaleAndTz( // App decorator new AppSession( // Base App implementation new AppBase() ) ); $action = // Action decorator new ActionAuthorized( // Base Action implementation new NumbersListJson(), new GuestAccessLvl() ); $app->start($action, $env, $user);
Verse 使用动作域响应者模式。简单动作示例
<?php namespace Actions; class NumbersListJson implements Action { public function run(Env $env, User $user): Response { return new RespJson([ 'result' => 'ok', 'list' => [1, 2, 3, 4] ]); } }
路由
路由存储在yaml文件中,例如
/auth: Auth/AuthForm /auth/process POST: Auth/AuthProcess
以"/"开头的每一行都是一个单独的路由。默认HTTP方法是GET。您可以定义路由如下
/auth: Auth/AuthForm
或如下
/auth GET: Auth/AuthForm
您可以使用令牌从URI中检索变量
/blog/read/{id}: Blog/ReadPost
甚至
/blog/list/{tag}/{author}: Blog/ListPosts
在动作中使用令牌
class ListPosts implements Action { public function run(Env $env, User $user): Response { $tag = $env->route()->token('tag'); $author = $env->route()->token('author'); return new Response\RespHtml($env->tpl()->render( 'blog/list', [ 'posts' => (new BlogPosts($tag, $author))->list(), 'tag' => $tag, 'author' => $author ] )); } }
待定