定制 fatcode/http-server 二次开发

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

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

fatcode/http-server

Composer 安装命令:

composer require fatcode/http-server

包简介

Fast, reliable, psr-15, psr-7 compatible php http server.

README 文档

README

Requirements

  • >= PHP 7.2
  • swoole extension
  • zlib extension

Installation

composer install fatcode/http-server

Quick start

<?php declare(strict_types=1);

use FatCode\HttpServer\HttpServer;
use FatCode\HttpServer\Server\Router;
use FatCode\HttpServer\Response;

// Instantiates router for registering resources:
$router = new Router();
$router->get('/hello', function () {
    return new Response('Hello You!');
});

// Run server at localhost:80
$server = new HttpServer();
$server->use($router);
$server->start();

The above example creates server that uses router with registered one resource. Server will listen on localhost at port 8080.

Please Note: Package is supporting PSR-7, that means all your registered handlers should expect ServerRequestInterface as an input, and return ResponseInterface as a result.

Running server as a daemon

Http server provides flexible configuration class, depending on your settings server can be daemonized, run on specific port, listen to specific amount of incoming connections and so on.

More options can be found in the class docblock itself.

<?php declare(strict_types=1);

use FatCode\HttpServer\HttpServer;
use FatCode\HttpServer\Server\HttpServerSettings;

// Setting pid file will make server run as a daemon.
$settings = new HttpServerSettings('0.0.0.0', 8080);
$settings->setPidFile(sys_get_temp_dir() . '/my_pid.pid');

// Note this server will always respond with 404 response, as there is
// no router passed that can handle the request.
$server = new HttpServer();
$server->start();

Middleware and PSR-15 support

Registering and using PSR-15 compatible middleware is trivial, just pass an instance of given middleware or closure itself to HttpServer::use method in the right order. In fact FatCode\HttpServer\Server\Router class is PSR-15 middleware itself.

<?php declare(strict_types=1);

use FatCode\HttpServer\HttpServer;
use FatCode\HttpServer\Response;
use Psr\Http\Message\ServerRequestInterface;

// Simple pong server.
$server = new HttpServer();
$server->use(function (ServerRequestInterface $request) : Response {
    if ($request->getUri()->getPath() === '/ping') {
        return new Response('Pong!');
    }
    return new Response('Please call /ping uri.');
});
$server->start();

Request, Response and PSR-7

Http package provides convenient PSR-7 implementation based on zendframework/zend-diactoros package. Shortly saying FatCode\HttpServer\ServerRequest and FatCode\HttpServer\Response objects utilize zend-diactoros exceptions and interface with some additions. When working with these objects please keep it in mind.

Creating new response object

<?php declare(strict_types=1);

use FatCode\HttpServer\Response;
use FatCode\HttpServer\HttpStatusCode;

// Creates new response with status code 200.
$response = new Response("Hello world!", HttpStatusCode::OK());

For your convenience package declares HttpStatusCode enum that helps with generation valid http responses. More status codes can be found in the class itself..

Working with request object

Reading route parameters

When declaring parametrized route you can access retrieve the value by calling FatCode\HttpServer\ServerRequest::getAttribute method. Consider the following example:

<?php declare(strict_types=1);

use FatCode\HttpServer\HttpServer;
use Psr\Http\Message\ServerRequestInterface;
use FatCode\HttpServer\Response;
use FatCode\HttpServer\Server\Router;

// Initialize router
$router = new Router();

// Register parametrized route
$router->get('/hello/{name}', function (ServerRequestInterface $request) : Response {
    // Return response
    return new Response("Hello, your name is {$request->getAttribute('name')}");
});

// Setup http server
$server = new HttpServer();
$server->use($router);
$server->start();

For more examples you can visit examples directory.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固