A minimal middleware based micro framework using PHP Framework Interop Group - PSR, where the goal is to achieve the best combination of flexibility and simplicity by using standards.
- Basic Coding Standard (1)
- Coding Style Guide (2)
- Logger Interface (3)
- Autoloading Standard (4)
- HTTP Message Interface (7)
- Container Interface (11)
- HTTP Handlers (15)
- HTTP Factories (17)
- php: ^7.2
- psr/container: ^1.0
- psr/http-factory: ^1.0.1
- psr/http-message-implementation: ^1.0
- psr/http-message: ^1.0.1
- psr/http-server-handler: ^1.0.1
- psr/http-server-middleware: ^1.0.1
- psr/log: ^1.1
Any Router which implements Chubbyphp\Framework\Router\RouterInterface can be used.
- aura/router: ^3.1
- nikic/fast-route: ^1.0|^0.6
- sunrise/http-router: ^2.0
- bittyphp/http: ^2.0
- guzzlehttp/psr7: ^1.4.2 (with http-interop/http-factory-guzzle: ^1.0)
- nyholm/psr7: ^1.0
- slim/psr7: ^0.5
- sunrise/http-message: ^1.0 (with sunrise/http-factory: ^1.0)
- laminas/laminas-diactoros: ^2.0
Through Composer as chubbyphp/chubbyphp-framework.
composer require chubbyphp/chubbyphp-framework "^2.6" aura/router "^3.1" slim/psr7 "^0.6"<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\AuraRouter;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
$loader = require __DIR__.'/vendor/autoload.php';
set_error_handler([new ErrorHandler(), 'errorToException']);
$responseFactory = new ResponseFactory();
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new AuraRouter([
Route::get('/hello/{name}', 'hello', new CallbackRequestHandler(
function (ServerRequestInterface $request) use ($responseFactory) {
$name = $request->getAttribute('name');
$response = $responseFactory->createResponse();
$response->getBody()->write(sprintf('Hello, %s', $name));
return $response;
}
))->pathOptions(['tokens' => ['name' => '[a-z]+']])
]), $responseFactory),
]);
$app->send($app->handle((new ServerRequestFactory())->createFromGlobals()));composer require chubbyphp/chubbyphp-framework "^2.6" nikic/fast-route "^1.3" slim/psr7 "^0.6"<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\FastRouteRouter;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
$loader = require __DIR__.'/vendor/autoload.php';
set_error_handler([new ErrorHandler(), 'errorToException']);
$responseFactory = new ResponseFactory();
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new FastRouteRouter([
Route::get('/hello/{name:[a-z]+}', 'hello', new CallbackRequestHandler(
function (ServerRequestInterface $request) use ($responseFactory) {
$name = $request->getAttribute('name');
$response = $responseFactory->createResponse();
$response->getBody()->write(sprintf('Hello, %s', $name));
return $response;
}
))
]), $responseFactory),
]);
$app->send($app->handle((new ServerRequestFactory())->createFromGlobals()));composer require chubbyphp/chubbyphp-framework "^2.6" sunrise/http-router "^2.0" slim/psr7 "^0.6"<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\SunriseRouter;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
$loader = require __DIR__.'/vendor/autoload.php';
set_error_handler([new ErrorHandler(), 'errorToException']);
$responseFactory = new ResponseFactory();
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new SunriseRouter([
Route::get('/hello/{name<[a-z]+>}', 'hello', new CallbackRequestHandler(
function (ServerRequestInterface $request) use ($responseFactory) {
$name = $request->getAttribute('name');
$response = $responseFactory->createResponse();
$response->getBody()->write(sprintf('Hello, %s', $name));
return $response;
}
))
]), $responseFactory),
]);
$app->send($app->handle((new ServerRequestFactory())->createFromGlobals()));This is an example of middleware(s) before and after the routing was done.
If you need to be able to continue without finding a route, I recommend writing a RouterMiddleware that will pass either the route or the RouteException and at the end another middleware that will convert the RouteException to a http response.
<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\CallbackMiddleware;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\FastRouteRouter;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
$loader = require __DIR__.'/vendor/autoload.php';
set_error_handler([new ErrorHandler(), 'errorToException']);
$responseFactory = new ResponseFactory();
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new CallbackMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
return $handler->handle($request);
}),
new RouterMiddleware(
new FastRouteRouter([
Route::get('/hello/{name:[a-z]+}', 'hello', new CallbackRequestHandler(
function (ServerRequestInterface $request) use ($responseFactory) {
$name = $request->getAttribute('name');
$response = $responseFactory->createResponse();
$response->getBody()->write(sprintf('Hello, %s', $name));
return $response;
}
))
]),
$responseFactory
),
new CallbackMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
/** @var Route $route */
$route = $request->getAttribute('route');
if ('hello' === $route->getName()) {
$request = $request->withAttribute('name', 'world');
}
return $handler->handle($request);
}),
]);
$app->send($app->handle((new ServerRequestFactory())->createFromGlobals()));- CallbackMiddleware
- ExceptionMiddleware
- LazyMiddleware
- MiddlewareDispatcher
- NewRelicRouteMiddleware
- RouterMiddleware
<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use React\EventLoop\Factory;
use React\Http\Server;
use React\Socket\Server as Socket;
/** @var Application $app*/
$app = ...;
$loop = Factory::create();
$socket = new Socket(8080, $loop);
$server = new Server($app);
$server->listen($socket);
$loop->run();<?php
namespace App;
use Chubbyphp\Framework\Application;
use Spiral\Goridge\StreamRelay;
use Spiral\RoadRunner\Worker;
use Spiral\RoadRunner\PSR7Client;
ini_set('display_errors', 'stderr');
/** @var Application $app */
$app = ...;
$worker = new Worker(new StreamRelay(STDIN, STDOUT));
$psr7 = new PSR7Client($worker);
while ($req = $psr7->acceptRequest()) {
try {
$psr7->respond($app->handle($req));
} catch (\Throwable $e) {
$psr7->getWorker()->error((string)$e);
}
}Replace the code from the first block with the code of the second ones.
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ExceptionHandler;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\Router\FastRouteRouter;
$app = new Application(
new FastRouteRouter([$route]),
new MiddlewareDispatcher(),
new ExceptionHandler($responseFactory, true)
);use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\Router\FastRouteRouter;
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new FastRouteRouter([$route]), $responseFactory),
]);Dominik Zogg 2019



