malik/crimson

一个用于处理HTTP请求的PHP库。

v1.0.1 2020-01-14 09:17 UTC

This package is auto-updated.

Last update: 2024-09-21 21:11:51 UTC


README

一个以干净方式处理HTTP请求的PHP库。此库基于ReactPHP构建。

安装

可以使用以下命令使用composer安装crimson

composer require malik/crimson:^1.0.0

用法

以下示例代码在通过GET请求访问https://:8080/foohttp://127.0.0.1:8080/foo时显示消息'Hello, World!!'

<?php

use Crimson\RequestHandler;
use Crimson\HttpServer;
use Crimson\App;

/**
 * Every handler class must extend the Crimson\RequestHandler class and
 * override methods.
 */
class GetHandler extends RequestHandler {

  private $msg;

  /**
   * The first method to be called from the handler class. Use this method to do
   * initialization instead of using a constructor.
   */
  public function initialize() {
    $this->msg = $this->getClassArgs()['msg'];
  }

  /**
   * Called before the request methods.
   */
  public function prepare() {
  }

  /**
   * Called when the request method is GET.
   */
  public function get() {
    $this->setStatus(200);
    $this->setContentType('text/plain');
    $this->write($this->msg);
  }

  /**
   * Use this method to set the response headers
   */
  public function setDefaultHeaders() {
    $this->setHeader('Access-Control-Allow-Origin', '*');
  }

  /**
   * This method is called at the end of the request.
   */
  public function onFinish() {
  }
}

/**
 * The Crimson\App class takes only 1 argument that is an array.
 *
 * Each element of the App class argument should be an array with 3 elements:
 *   1. RegEx pattern representing the path to invoke the handler class methods.
 *   2. Name of the handler class.
 *   3. An array which will be passed to the constructor of the handler class
 *      and can be accessed by `getClassArgs()` method.
 */
$app = new App([
  ['\/foo', 'GetHandler', ['msg' => 'Hello, World!!']]
]);

/**
 * The HttpServer class takes 4 arguments:
 *   1. The instance of the App class(required).
 *   2. Array of TLS options.
 *   3. Address to access the Http server.
 *   4. The port on which the server listens to.
 */
$server = new HttpServer($app, [], '127.0.0.1', '8080');

// Start the server
$server->start();

更多示例,请查看此存储库中的examples/目录。

许可证

GNU通用公共许可证v2.0