pixo / outpost
分离式网站开发的 PHP 框架
Requires
- php: >=5.5
- guzzlehttp/guzzle: ^6.2
- monolog/monolog: ^1.19
- phpdocumentor/reflection-docblock: ^3.1
- phroute/phroute: 2.*
- symfony/http-foundation: ~2.0
- tedivm/stash: 0.12.*
- twig/twig: ^1.26
- wp-cli/php-cli-tools: ^0.11.1
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-20 11:04:43 UTC
README
Outpost 是一个轻量级 Web 框架,专为 分离式 网站 设计。需要 PHP 5.5+。
Outpost 能做什么
路由。 Outpost 使用 Phroute 将传入的请求路由到正确的响应器。
缓存。 每个 Outpost 网站都有一个 Stash 实例,用于在请求之间存储资源。
日志。 Outpost 使用 Monolog 将状态消息发送到各种类型的日志。
Outpost 不能做什么
模板。 Outpost 不提供默认的模板引擎。
HTTP。 Outpost 不提供客户端来获取外部资源。
快速入门
为您的 Outpost 安装创建一个新的目录。在此目录内部,使用 Composer 安装 Outpost
composer require pixo/outpost
现在您应该有 composer.json
和 composer.lock
文件,以及一个包含 Outpost 和其依赖项的 vendor
目录。
创建一个名为 public
的新目录,并在该目录中创建一个名为 index.php
的新文件,内容如下
<?php # Get the Composer autoloader require_once __DIR__ . '/../vendor/autoload.php'; # Get the incoming request $request = Symfony\Component\HttpFoundation\Request::createFromGlobals(); # Create a Site object $site = new Outpost\Site(); # Add an example route $site->addRoute('GET', '/', function () { print "Hello."; }); # Send a response $site->respond($request);
启动一个本地的 PHP 开发服务器 并将其指向 public
目录
php -S 0.0.0.0:8080 -t public
一旦服务器启动,您应该可以访问 https://:8080/ 并看到以下内容
Hello.
发生了什么?
Outpost 收到了对主页的请求,并将其路由到打印 "Hello" 的函数。
当您访问 https://:8080/ 时,服务器将您重定向到 index.php
脚本。它包含 Composer 自动加载器,然后使用来自服务器环境的信息创建了一个新的 Request 对象。
脚本接下来创建了一个 Outpost Site 对象,并添加了一个路由指令:当访客请求主页时,运行此函数。用作路由目标的功能或其他 callables 被称为 Responders。
最后,调用了新 Site 的 respond()
方法。路由器使用 Request 对象来找到正确的 Responder:一个打印 "Hello" 的函数。
网站
网站对象有两个主要用途
- 它们将每个传入的请求路由到适当的响应器。
- 它们提供创建响应所需的资源。
响应器
响应器作为路由回调执行,并在调用时预期输出响应。
可以使用网站的 addRoute()
方法创建响应器路由
$site->addRoute('GET', '/news', new NewsPageResponder()); $site->addRoute('GET', '/news/article/{id}', new ArticlePageResponder());
响应器在调用时接收 3 个参数
- 响应请求的网站对象
- 请求对象
- 从 URL 中提取的任何参数
class ArticlePageResponder { public function __invoke($site, $request, array $params) { $articleId = list($params); print $this->render('about-page.tpl', $this->getArticle($articleId)); } }
资源
Outpost 安装可以定义任意数量的资源类。资源通过网站的 get()
方法检索,并在调用时接收网站对象。最简单的资源只是一个可调用对象
$resource = function ($site) { return 1; } print $site->get($resource);
get()
方法调用可调用对象并返回结果,因此输出将是
1
缓存
实现 CacheableInterface
接口的资源可以存储在站点缓存中,并且只有在缓存资源缺失或过时时才会被调用。可缓存资源有一个唯一的键,并指定它们在需要刷新之前可以被缓存的秒数。
class ExampleExpensiveResource implements \Outpost\Cache\CacheableInterface { public function __invoke($site) { # Something that takes a long time, then... return $this; } public function getCacheKey() { return 'examples/expensive'; } public function getCacheLifetime() { return 3600; # 1 hour } }
第一次请求此资源时,会调用它,并将返回值存储在站点缓存中。对于随后的请求,返回缓存的副本,直到副本的年龄超过 getCacheLifetime()
的值。
# Nothing in the cache for this call, so Outpost invokes the Resource # and caches the return value. $fresh = $site->get(new ExampleExpensiveResource()); # This time the Resource is in the cache, so Outpost returns the cached Resource. $cached = $site->get(new ExampleExpensiveResource()); # An hour passes... # Now the cached copy is stale, so Outpost will invoke the Resource again, # and replace the cached copy. $fresh = $site->get(new ExampleExpensiveResource());
Site::getCache()
方法提供了对底层 Stash 缓存对象的访问。
# Clear a specific key $site->getCache()->clear('cache/key'); # Clear a range of keys $site->getCache()->clear('things/id/*'); # Clear the whole cache $site->getCache()->clear(); # Flush the cache $site->getCache()->flush();