定制 malik/crimson 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

malik/crimson

Composer 安装命令:

composer require malik/crimson

包简介

A PHP Library to handle the HTTP requests.

README 文档

README

A PHP Library to handle the HTTP requests in a clean way. This library is built on top of ReactPHP.

Installation

The crimson can be installed using composer via the following command:

composer require malik/crimson:^1.0.0

Usage

The following example code display the message 'Hello, World!!' when http://localhost:8080/foo or http://127.0.0.1:8080/foo is accessed with the GET request.

<?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();

For more examples, check the examples/ directory in this repository.

License

GNU General Public License v2.0

统计信息

  • 总下载量: 81
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-only
  • 更新时间: 2020-01-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固