A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework. Although performance was not a focus, it's currently the fastest PSR-15 based framework (php-fpm) on the market.
- 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.
- chubbyphp/chubbyphp-framework-router-aura: ^1.0
- chubbyphp/chubbyphp-framework-router-fastroute: ^1.0
- chubbyphp/chubbyphp-framework-router-sunrise: ^1.0
- chubbyphp/chubbyphp-framework-router-symfony: ^1.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|^1.0
- 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 "^3.1" \
chubbyphp/chubbyphp-framework-router-aura "^1.0" \
slim/psr7 "^1.0"composer require chubbyphp/chubbyphp-framework "^3.1" \
chubbyphp/chubbyphp-framework-router-fastroute "^1.0" \
slim/psr7 "^1.0"composer require chubbyphp/chubbyphp-framework "^3.1" \
chubbyphp/chubbyphp-framework-router-sunrise "^1.0" \
slim/psr7 "^1.0"composer require chubbyphp/chubbyphp-framework "^3.1" \
chubbyphp/chubbyphp-framework-router-symfony "^1.0" \
slim/psr7 "^1.0"<?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\FastRoute\Router;
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 Router([
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->emit($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\FastRoute\Router;
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 Router([
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->emit($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);
}
}Removed the following methods on group: group, route, middlewares, middleware, pathOptions, use additional parameters.
Removed the following methods on route: middlewares, middleware, pathOptions, use additional parameters.
Group::create('/{id}')
->pathOptions(['tokens' => ['id' => '\d+']])
->middleware($middleware1)
->route(Route::get('/{slug}', 'element_read', $handler)
->pathOptions(['tokens' => ['slug' => '[a-z]+']])
->middleware($middleware2)
)
->group(Group::create('/{slug}')
->pathOptions(['tokens' => ['slug' => '[a-z]+']])
->middlewares([$middleware2])
->route(Route::get('/{key}', 'another_route', $handler)
->pathOptions(['tokens' => ['key' => '[a-z]+']])
->middleware($middleware3)
)
)
->route(Route::get('/{slug}/{key}/{subKey}', 'yet_another_route', $handler)
->pathOptions(['tokens' => ['slug' => '[a-z]+', 'key' => '[a-z]+', 'subKey' => '[a-z]+']])
->middleware($middleware2)
)
;Group::create('/{id}', [
Route::get(
'/{slug}',
'element_read',
$handler,
[$middleware2],
['tokens' => ['slug' => '[a-z]+']]
),
Group::create('/{slug}', [
Route::get(
'/{key}',
'another_route',
$handler,
[$middleware3],
['tokens' => ['key' => '[a-z]+']]
),
], [$middleware2], ['tokens' => ['slug' => '[a-z]+']]),
Route::get(
'/{slug}/{key}/{subKey}',
'yet_another_route',
$handler,
[$middleware2],
['tokens' => ['slug' => '[a-z]+', 'key' => '[a-z]+', 'subKey' => '[a-z]+']]
),
], [$middleware1], ['tokens' => ['id' => '\d+']]);- Upgrade:
composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-aura - Replace
Chubbyphp\Framework\Router\AuraRouterwithChubbyphp\Framework\Router\Aura\Router.
- Upgrade:
composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-fastroute - Replace
Chubbyphp\Framework\Router\FastRouteRouterwithChubbyphp\Framework\Router\FastRoute\Router.
- Upgrade:
composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-sunrise - Replace
Chubbyphp\Framework\Router\SunriseRouterwithChubbyphp\Framework\Router\Sunrise\Router.
- Upgrade:
composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-symfony - Replace
Chubbyphp\Framework\Router\SymfonyRouterwithChubbyphp\Framework\Router\Symfony\Router.
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 2020



