mariuslundgard/php-server

PHP的一个最小化路由HTTP中间件框架。

v0.1.1-beta.1 2014-08-09 19:10 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:08:03 UTC


README

Build Status Coverage Status

Latest Stable Version

特性

  • 可路由的中间件(应用层)
  • 可路由的控制器操作

示例

这是标准的 你好世界 示例

<?php 

require 'vendor/autoload.php';

$app = (new Server\Module())
	->map([
		'fn' => function ($req, $res) {
			$res->write('<h1>Hello, world</h1>');
		}
	])
	->call()  // calls the application
	->send(); // outputs the headers and response body

此示例展示了如何使用中间件和映射控制器

<?php

require 'vendor/autoload.php';

class HeaderFilter extends Server\Layer
{
    public function call(Server\Request $req = null, Server\Error $err = null)
    {
        $res = parent::call($req, $err);

        $res->body = '<h1>'.$res->body.'</h1>';

        return $res;
    }
}

class FrontController extends Server\Controller
{
    public function index()
    {
        return 'Hello, world!';
    }
}

$app = new Server\Module();

$app->employ([
    'class' => 'HeaderFilter',
]);

$app->map([
    'controller' => 'FrontController',
]);

$app->call()->send(); // outputs: <h1>Hello, world!</h1>