jontynewman / oku
v1.0.1
2018-11-01 22:09 UTC
Requires
- php: >=7.1
- psr/http-server-handler: ^1.0
- shroophp/psr: ^1.3
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^4.8
Suggests
- jontynewman/oku-aggregate: Functionality for aggregating multiple repositories into a single repository.
- jontynewman/oku-io: Functionality for generating output from set input per path.
- jontynewman/oku-upload: Allows files to be uploaded and served.
This package is not auto-updated.
Last update: 2024-09-29 10:22:47 UTC
README
一个简单的内容管理系统。
安装
composer require 'jontynewman/oku ^1.0'
示例使用
你好,世界!
在以下示例中,对根路径(即 /
)的请求将响应为纯文本响应 'Hello, <path>!'
,其中 <path>
是请求的路径。所有其他路径将响应为 404 Not Found
。
假设请求的方法和路径将分别从 $_SERVER['REQUEST_METHOD']
和 $_SERVER['REQUEST_URI']
读取。
<?php
use GuzzleHttp\Psr7\Response;
use JontyNewman\Oku\ContextInterface;
use JontyNewman\Oku\RequestHandler;
use JontyNewman\Oku\ResponseBuilderInterface;
require 'vendor/autoload.php';
$root = function (
ResponseBuilderInterface $builder,
ContextInterface $context
) {
$builder->code(200);
$builder->headers(['Content-Type' => 'text/plain']);
$builder->content(function () use ($context) {
echo "Hello, '{$context->request()->getUri()->getPath()}'!";
});
};
$repository = new ArrayObject(['/' => $root]);
$default = new Response(404, ['Content-Type' => 'text/plain'], 'Not Found');
$handler = new RequestHandler($repository, $default);
$handler->run();
编辑器
为了启用内容管理,必须将编辑器传递给请求处理器。
$editor = function (
ResponseBuilderInterface $builder,
ContextInterface $context
) {
$builder->content(function () use ($context) {
require 'editor.php';
});
};
$handler = new RequestHandler($repository, $default, $editor);
以下是一个编辑器示例模板(即 editor.php
)。
<?php /* @var $context \JontyNewman\Oku\ContextInterface */ ?>
<!DOCTYPE html>
<html>
<head>
<title>JontyNewman\Oku</title>
</head>
<body>
<?php /* Allow the content to be updated. */ ?>
<form action="" method="post">
<p>
<label>
Text
<textarea name="text"></textarea>
</label>
</p>
<p>
<?php /* Prevent cross-site request forgery. */ ?>
<?= $context->token(); ?>
<?php /* Override POST with PUT. */ ?>
<?= $context->put(); ?>
<input type="submit" value="Update">
</p>
</form>
<?php /* Allow the content to be deleted. */ ?>
<form action="" method="post">
<p>
<?php /* Prevent cross-site request forgery. */ ?>
<?= $context->token(); ?>
<?php /* Override POST with DELETE. */ ?>
<?= $context->delete(); ?>
<input type="submit" value="Delete">
</p>
</form>
<?php /* Render the current content in an iframe. */ ?>
<?= $context->inset(); ?>
</body>
</html>
仓库
仓库是实现 \ArrayAccess
接口的对象。
GET
请求将检索与请求路径等效的偏移处的值。预期的返回值是一个将接受一个 \JontyNewman\Oku\ResponseBuilderInterface
作为第一个参数和一个 \JontyNewman\Oku\ContextInterface
作为第二个参数的 callable
。
PUT
请求将在与请求路径等效的偏移处设置值,作为 PSR-7 请求。
DELETE
请求将取消与请求路径等效的偏移处的设置。
总之,PSR-7 请求进入仓库,而 callable
值从仓库中出来。
鸡进去了,派出来了。
——特威迪太太,2000年
持久化仓库的方法应由开发者确定。
配置
推荐配置是将所有请求重写到单个 PHP 文件(通常命名为 index.php
),请求路径通过服务器 API 传递到 $_SERVER['REQUEST_URI']
。
更多信息,请参阅 ShrooPHP\Framework 配置。