此包已被废弃且不再维护。未建议替代包。

类似Rack的接口。

v3.6.0 2021-06-18 17:01 UTC

README

Latest stable version Build status Code coverage License

Shelf是一个为PHP >=8提供的类似Rack的接口,但目前不再积极维护。

如果您需要在PHP中处理请求和响应,请考虑使用符合PSR-7规范的库。😄

入门

安装

Shelf可通过Composer & Packagist获取。

{
  "require": {
    "oscarpalmer/shelf": "3.6.*"
  }
}

基本用法

以下有两个简单的示例,帮助您在约十秒钟内开始使用。如果您遇到困难或想了解更多信息,请参考API参考

请求

use oscarpalmer\Shelf\Request;

$request = new Request($server); # Or new Request::fromGlobals();

echo $request->path_info;

响应

use oscarpalmer\Shelf\Response;

$response = new Response(
    'Hello, world!',
    200,
    ['Content-Type' => 'text/plain']
);

$response->finish($request);

API

Shelf

# Shelf version
Shelf::VERSION

请求

# Constructor
# Takes an array of server variables and a session variable;
# the session variable can be either boolean (to enable/disable sessions),
# or a string (to enable a session with a unique name)
$request = new Shelf\Request($server, $session);

# Check if HTTP request matches an expected type
$request->isDelete();
$request->isGet();
$request->isHead();
$request->isOptions();
$request->isPatch();
$request->isPost();
$request->isPut();

# Check if HTTP request was made via AJAX
$request->isAjax();

# Getters for Blobs (described below) for accessing HTTP request information
$request->getCookies(); # $_COOKIES
$request->getData();    # $_POST
$request->getQuery();   # $_GET
$request->getServer();  # $_SERVER or custom server variables
$request->getSession(); # $_SESSION

# Getter for uploaded files; a more detailed description can be found below
$request->getFiles(); # $_FILES

# Alternative to using the constructor; automatically uses the $_SERVER-variables
# The session variable still works the same :)
Shelf\Request::fromGlobals($session);

响应

# Constructor
# Takes a scalar body, an HTTP status code, and an array of HTTP headers
$response = new Shelf\Response($body, $status, $headers);

# Retrieves the response body as a string
$response->getBody();

# Retrieves the value for a header
$response->getHeader();

# Retrieves all headers
$response->getHeaders();

# Retrieves the status code 
$response->getStatus();

# Retrieves a status message for the current response, e.g. '200 OK'
$response->getStatusMessage();
$response->getStatusMessage($code); # Or retrieve a specific status message

# Set a scalar value as the response body
$response->setBody($body);

# Set a response header
$response->setHeader($key, $value);

# Set multiple respons headers
$response->setHeaders($headers);

# Set response status
$response->setStatus($status);

# Append scalar value to the response body
$response->write($content);

文件

上传的文件可以通过$request->getFiles()访问,它返回一个包含每个文件的File对象的Files对象。

# Files

$files->name;              # Returns a File, or array of Files
$files->get('name');       # A less magical version of the above

# File

$file->getError();         # Error code for uploaded file
$file->getName();          # Original file name for uploaded file
$file->getSize();          # File size for uploaded file
$file->getTemporaryName(); # Temporary file name for uploaded file
$file->getType();          # File type for uploaded file

Blob

Blob是用于存储任何类型可迭代数据的容器。在Request类中,Blob用于管理$_COOKIES$_FILES$_GET$_POST$_SERVER(或自定义服务器变量)和$_SESSION信息。在Response类中,Blob用于管理HTTP头。

# Retrieve all Blob values as an array
$blob->all();

# Delete a value by key
$blob->delete($key);

# Retrieve a value by key with an optional default value
$blob->get($key, $default);

# Check if Blob has key
$blob->has($key);

# Set value by key
$blob->set($key, $value);

许可证

MIT许可;更多信息请参阅LICENSE文件